For the simple case of vectors, expand.grid
exists
v1 <- 1:2 v2 <- 3:4 expand.grid(v1, v2) # Var1 Var2 #1 1 3 #2 2 3 #3 1 4 #4 2 4
I do not know about a function that will automatically do what you want to do for dataframes (see edit)
We could do this relatively easily using expand.grid and cbind.
df1 <- data.frame(a = 1:2, b=3:4) df2 <- data.frame(cat = 5:6, dog = c("a","b")) expand.grid(df1, df2)
Edit: As Joran notes in the comments, merge
does this for us for data frames.
df1 <- data.frame(a = 1:2, b=3:4) df2 <- data.frame(cat = 5:6, dog = c("a","b")) merge(df1, df2)
source share