I have two different length lists and would like to generate all the permutations to run through a function in R. I can do it with a for loop (see below) but I don't like using rbind and cbind. I haven't been able to get it to work with *apply or purrr functions like map2 because they complain about unequal lengths.
What is the cleanest tidyverse-friendly way to do this?
Simplified example below:
myfun = function(a,b){
return(a*b)
}
xvalues = c(1,2,3)
yvalues = c(10,20,30,40)
merged = c()
for (x in xvalues){
z = myfun(x,yvalues)
merged = rbind(merged,(cbind(x,yvalues,z)))
}
df = data.frame(merged)
This generates the desired dataframe:
x yvalues z
1 1 10 10
2 1 20 20
3 1 30 30
4 1 40 40
5 2 10 20
6 2 20 40
7 2 30 60
8 2 40 80
9 3 10 30
10 3 20 60
11 3 30 90
12 3 40 120