0

To create unique combinations, I used to use below SAS code, I would like to know R equivalent of this. Could anyone please help me doing the same in R, I'm very new to R, just exploring.

DATA B ;

DO i = 1 to 2 ;

DO j = 1 to 5 ;

DO k = 1 to 4 ;

OUTPUT ;

END ;

END ;

END ;

RUN ;

DATA B ;

SET  B ;

IJK = CATX("-",i,j,k) ;

RUN ;

this will give me the following output:

Obs i j k ijk

1 1 1 1 1-1-1

2 1 1 2 1-1-2

3 1 1 3 1-1-3 

4 1 1 4 1-1-4 

5 1 2 1 1-2-1 

...........
etc
0

2 Answers 2

1
require(dplyr)
expand.grid(i = 1:2, j = 1:5, k = 1:4) %>% 
  arrange(i, j, k) %>% 
  mutate(ijk = paste(i, j, k, sep = '-'))
Sign up to request clarification or add additional context in comments.

2 Comments

I have 1 more similar question. ID Type Sales 1 1 $ 5,027 2 1 $ 2,646 3 1 $ 7,549 4 2 $ 4,536 5 2 $ 3,118 6 3 $ 9,815 7 3 $ 885 8 3 $ 2,911 I can split the above dataset by Type wise by using the following macro %MACRO split; %DO m = 1 %TO 6 ; DATA type_%eval(&m) ; SET main ; IF Type = &m then output type_%eval(&m) ; RUN ; %END ; %MEND split ; %split ; ID Type Sales 1 1 $ 5,027 2 1 $ 2,646 3 1 $ 7,549 ID Type Sales 4 2 $ 4,536 5 2 $ 3,118 and etc., Could you please explain how to do the same in R? Thanks in advance
You should ask this as a separate question
0

Are you attempting something like this?

set.seed(2)

Obs <- 1:20
i <- sample(1:2, 20, replace = TRUE)
j <- sample(1:5, 20, replace = TRUE)
k <- sample(1:4, 20, replace = TRUE)
ijk <- paste(i,j,k, sep="-")
B <- data.frame(Obs,i,j,k,ijk); B

#    Obs i j k   ijk
# 1    1 1 4 4 1-4-4
# 2    2 2 2 2 2-2-2
# 3    3 2 5 1 2-5-1
# 4    4 1 1 1 1-1-1
# 5    5 2 2 4 2-2-4
# 6    6 2 3 4 2-3-4
# 7    7 1 1 4 1-1-4
# 8    8 2 2 2 2-2-2
# 9    9 1 5 3 1-5-3
# 10  10 2 1 4 2-1-4
# 11  11 2 1 1 2-1-1
# 12  12 1 1 1 1-1-1
# 13  13 2 5 3 2-5-3
# 14  14 1 5 4 1-5-4
# 15  15 1 3 2 1-3-2
# 16  16 2 4 4 2-4-4
# 17  17 2 5 4 2-5-4
# 18  18 1 2 4 1-2-4
# 19  19 1 4 3 1-4-3
# 20  20 1 1 3 1-1-3

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.