I have a vector of characters v <- c("piment","aubergine","carotte","oignon","chou","pommeDeTerre")
and I would like to combine them to prepare a complete experimental design. So I want to produce a data.frame
with for each line a set of n
elements and as many lines as possible
v <- c("piment","aubergine","carotte","oignon","chou","pommeDeTerre")
n <- 12
## TEST 1 : crach R
tmp <- data.frame(matrix(rep(v,n), ncol = n))
expand.grid(tmp)
## TEST 2 :
temp = t(combn(rep(v,nbslot), nbslot))
#Error in matrix(r, nrow = len.r, ncol = count) :
# valeur 'ncol' incorrecte (trop grande ou NA)
#De plus : Warning message:
#In combn(rep(v, nbslot), nbslot) :
# NAs introduced by coercion to integer range
Seems to work for n <- 8
but not for n <- 12
. How to overpass this issue
You could use combinations
from gtools
package.
As an illustration with r=5
, but also works with r=12
:
library(gtools)
combinations(length(v),v,r=5,repeats.allowed = T)
[,1] [,2] [,3] [,4] [,5]
[1,] "aubergine" "aubergine" "aubergine" "aubergine" "aubergine"
[2,] "aubergine" "aubergine" "aubergine" "aubergine" "carotte"
[3,] "aubergine" "aubergine" "aubergine" "aubergine" "chou"
[4,] "aubergine" "aubergine" "aubergine" "aubergine" "oignon"
[5,] "aubergine" "aubergine" "aubergine" "aubergine" "piment"
[6,] "aubergine" "aubergine" "aubergine" "aubergine" "pommeDeTerre"
[7,] "aubergine" "aubergine" "aubergine" "carotte" "carotte"
...