Create 3 mutually exclusive samples in r -


i have data set need split 3 mutually exclusive random samples of different sizes. used:

testdata<-sample(47959,14388,replace=false,prob=null) 

to try , create sample (the data set size 47959) don't know how make sample manipulatable data set in r.

some data:

set.seed(42) x <- sample(20, size=100, replace=true) head(x) ## [1] 19 19  6 17 13 11 

random sizes

create index of 1:3, , use subset data:

i <- sample(1:3, size=length(x), replace=true) head(i) ## [1] 2 1 1 2 3 3 

now break 3 groups (many ways this):

x.grouped <- split(x, i) str(x.grouped) ## list of 3 ##  $ 1: int [1:31] 19 6 15 20 9 5 8 9 18 20 ... ##  $ 2: int [1:30] 19 17 14 10 6 10 19 3 10 19 ... ##  $ 3: int [1:39] 13 11 15 3 15 19 12 2 8 19 ... 

the relative sizes of 3 groups vary randomly.

controlled sizes

indices represents size desire in each group.

indices <- c(20, 50, 30) indices.cs <- cumsum(indices) x.unsorted <- sample(x) xs.grouped.sized <- mapply(function(a,b) x.unsorted[a:b],     1+lag(indices.cs, default=0),     indices.cs,     simplify=false) str(xs.grouped.sized) ## list of 3 ##  $ : int [1:20] 2 7 13 1 19 7 14 20 19 1 ... ##  $ : int [1:50] 13 6 19 4 19 20 20 11 17 3 ... ##  $ : int [1:30] 1 10 7 16 9 16 17 11 14 8 ... 

edit: updated implementation

indices <- sample(rep(1:3, times = c(20,50,30))) str(split(x, indices)) ## list of 3 ##  $ 1: int [1:20] 6 3 10 6 10 20 17 8 5 13 ... ##  $ 2: int [1:50] 19 19 17 15 14 15 19 20 3 19 ... ##  $ 3: int [1:30] 13 11 15 19 10 12 3 11 14 1 ... 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -