R - shuffle a list preserving element sizes -
in r, need efficient solution shuffle elements contained within list, preserving total number of elements, , local element sizes (in case, each element of list vector)
a<-letters[1:6] b<-letters[6:10] c<-letters[c(9:15)] l=list(a,b,c) > l [[1]] [1] "a" "b" "c" "d" "e" "f" [[2]] [1] "f" "g" "h" "i" "j" [[3]] [1] "i" "j" "k" "l" "m" "n" "o"
the shuffling should randomly select letters of list (without replacement) , put them in random position of vector within list.
i hope have been clear! :-)
you may try recreating second list skeleton of first, , fill elements of first list, this:
u<-unlist(l) l2<-relist(u[sample(length(u))],skeleton=l) > l2 [[1]] [1] "f" "a" "o" "i" "s" "q" [[2]] [1] "r" "p" "k" "f" "g" [[3]] [1] "a" "n" "m" "j" "h" "g" "e" "b" "t" "c" "d" "l"
hope helps!
Comments
Post a Comment