Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate combination of data frame and vector

Tags:

r

I know expand.grid is to create all combinations of given vectors. But is there a way to generate all combinations of a data frame and a vector by taking each row in the data frame as unique. For instance,

df <- data.frame(a = 1:3, b = 5:7)
c <- 9:10

how to create a new data frame that is the combination of df and c without expanding df:

df.c:
a b c
1 5 9
2 6 9
3 7 9
1 5 10
2 6 10
3 7 10

Thanks!

like image 940
Rock Avatar asked Nov 28 '12 01:11

Rock


1 Answers

As for me the simplest way is merge(df, as.data.frame(c))

  a b  c
1 1 5  9
2 2 6  9
3 3 7  9
4 1 5 10
5 2 6 10
6 3 7 10
like image 168
inscaven Avatar answered Sep 28 '22 07:09

inscaven