expand.grid()

Published by onesixx on

Create a Data Frame from All Combinations of Factor Variables

> expand.grid(1:3, 3:1)
  Var1 Var2
1    1    3
2    2    3
3    3    3
4    1    2
5    2    2
6    3    2
7    1    1
8    2    1
9    3    1
temp1 <- data.frame( x=expand.grid(1:3, 3:1)[,1],
                     y=expand.grid(1:3, 3:1)[,2] )

temp2 <- expand.grid(x=1:3, y=3:1)
#names(temp2) <- c("x","y")
attributes(temp2)$out.attrs <- NULL

> identical(temp1, temp2)
[1] TRUE
Categories: R Basic

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x