Combinations of Variables in R -


i'm trying create fake data frame examine effects multinomial logit model in r. have code precisely want do, wich create row representing every combination of levels of different variables.

var1 <- seq(1,10,1) var2 <- seq(1,20,5)  fakedata <- as.data.frame(matrix(na, nrow=length(var1) * length(var2),                                  ncol=2))  row <- 1 for(i in 1:length(var1)){   for(j in 1:length(var2)){     fakedata[row, 1] <- var1[i]      fakedata[row, 2] <- var2[j]     row <- row + 1   } }   > head(fakedata)   v1 v2 1  1  1 2  1  6 3  1 11 4  1 16 5  2  1 6  2  6 

my problem code inefficient when applied problem 4 variables of around ten levels each. tips on functions might make quicker?

you may looking expand.grid ?

r> expand.grid(var1, var2)    var1 var2 1     1    1 2     2    1 3     3    1 4     4    1 5     5    1 6     6    1 7     7    1 8     8    1 9     9    1 10   10    1 11    1    6 12    2    6 13    3    6 14    4    6 15    5    6 16    6    6 17    7    6 18    8    6 19    9    6 20   10    6 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -