Split a vector into three vectors of unequal length in R -


a questions relative n00b: i’d split vector 3 vectors of different lengths, values assigned each vector @ random. example, i’d split vector of length 12 below vectors of length 2,3, , 7

i can 3 equal sized vectors using this:

test<-1:12 split(test,sample(1:3)) 

any suggestions on how split test vectors of 2,3, , 7 instead of 3 vectors of length 4?

you use rep create indices each group , split based on that

split(1:12, rep(1:3, c(2, 3, 7))) 

if wanted items randomly assigned it's not first 2 items in first vector, next 3 items in second vector, ..., add call sample

split(1:12, sample(rep(1:3, c(2, 3, 7)))) 

if don't have specific lengths (2,3,7) in mind don't want equal length vectors every time simono101's answer way go.


Comments

Popular posts from this blog

Line ending issue with Mercurial or Visual Studio -

java - Jtable duplicate Rows -

java - Run a .jar on Heroku -