cut - randomly allocating factor variables into different groups using r -


i have list of 9 factor variables: item <- c("a","b","c","d","e","f","g","h","i"). randomly allocate each of 9 factor variables 3 groups. tried script might lot of work:

gp1 <- sample(item,3,replace=f) > gp1 [1] "b" "h" "g" 

i tried cut function variables have numeric:

gp <- cut(item, breaks = 3) error in cut.default(item, breaks = 3) : 'x' must numeric 

the expected output should variables randomly allocated each group:

grp1  grp1  grp3 d      b     c      e     h      g     f 

thank help!

just sample items , put in matrix (which can converted data.frame if desire):

matrix(sample(item), ncol = 3) ##      [,1] [,2] [,3] ## [1,] "b"  "d"  "a"  ## [2,] "f"  "i"  "e"  ## [3,] "h"  "g"  "c"  

if items won't split equal lengths, might consider split, splitting variable based on shuffling of how many groups want.

for instance:

item <- item[-c(1, 2)] split(item, sample(rep(1:3, length.out = length(item)))) ## $`1` ## [1] "c" "e" "i" ##  ## $`2` ## [1] "f" "g" ##  ## $`3` ## [1] "d" "h" ##  

Comments