Suppose I have these data
data1 <- read.delim(textConnection(
"id val1
1 blue
1 green
1 red
2 black
2 brown
2 white"
), sep=' ')
data2 <- read.delim(textConnection(
"id val2
1 cat
1 dog
1 fish
2 hat
2 coat
2 car"
), sep=' ')
I would like to calculate all permutations of blue, green, and red cat, dog, and fish for id=1 and brown, black, and white hats, coats, and cars for id=2. I could do it in a for loop with expand.grid, and then "build" the output using rbind. But my actual data have several IDs and several vals so it runs poorly.
>Solution :
In base R, we could use split on both the datasets to create a list of values by ‘id’ and then apply the expand.grid on the corresponding elements of the list and rbind (if needed)
Map(expand.grid, split(data1$val1, data1$id), split(data2$val2, data2$id))
Or in data.table
library(data.table)
setDT(data1)[data2, on = .(id), allow.cartesian = TRUE]