... except the matrix approach doesn't work if the length of the vector is not exactly divisible by the number of groups. That's why I used split.
Cheers, Bert Gunter Genentech Nonclinical Biostatistics -----Original Message----- From: David Winsemius [mailto:dwinsem...@comcast.net] Sent: Thursday, October 15, 2009 8:48 AM To: Bert Gunter Cc: 'Marcio Resende'; r-help@r-project.org Subject: Re: [R] Sampling procedure If parsimony is needed, then define a 9-row matrix and send a randomized indexed version of Example to it: s<-matrix(NA, nrow=9, ncol=length(Example)/9) s[,] <- Example[sample(Example, length(Example) )] > str(s) int [1:9, 1:87] 503 731 708 23 255 675 163 381 361 412 ... Or even: s<-matrix(Example[ sample(Example, length(Example) )], nrow=9, ncol=length(Example)/9) -- David On Oct 15, 2009, at 11:22 AM, Bert Gunter wrote: > If I understand what is wanted correctly, this can be a one-liner! > -- think > whole objects: > > splitup <- function(x,n.groups) > #split x into n.groups mutually exclusive sets > { > lx <- length(x) > if(n.groups >= lx) stop("Number of groups greater than vector > length") > x <- x[sample(lx,lx)] > split(x,seq_len(n.groups)) > } > > ## testit > >> splitup(1:71,9) > > $`1` > [1] 22 26 38 50 65 60 9 27 > > $`2` > [1] 24 2 69 28 71 31 41 13 > > $`3` > [1] 16 47 63 45 23 1 8 32 > > $`4` > [1] 34 39 64 35 7 19 4 55 > > $`5` > [1] 54 10 37 68 6 17 70 18 > > $`6` > [1] 61 11 5 46 33 43 14 56 > > $`7` > [1] 42 44 12 62 66 48 57 58 > > $`8` > [1] 21 40 30 29 20 49 52 67 > > $`9` > [1] 59 15 25 51 3 36 53 > > > Cheers, > > Bert Gunter > Genentech Nonclinical Statistics > > > -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org > ] On > Behalf Of David Winsemius > Sent: Thursday, October 15, 2009 7:55 AM > To: Marcio Resende > Cc: r-help@r-project.org > Subject: Re: [R] Sampling procedure > > > On Oct 15, 2009, at 10:19 AM, Marcio Resende wrote: > >> >> I would like to divide a vector in 9 groups in a way that each >> number is >> present in only one group. >> In a vector of 783 I would like to divide in 9 different groups of 87 >> >> Example <- matrix(c(1:783),ncol = 1) > > >> Example <- matrix(c(1:783),ncol = 1) >> Grp1 <- sample(Example, 87, replace=FALSE) >> Grp2 <- sample(Example[-Grp1], 87, replace=FALSE) >> Grp3 <- sample(Example[-c(Grp1, Grp2)], 87, replace=FALSE) > # lather, rinse , repeat > > >> s1 <- as.matrix(sample(Example,87, re = FALSE)) >> Example <- Example[-s1] >> s2 <- as.matrix(sample(Example,87, re = FALSE)) >> #however I don“t know how to remove the second group from the >> "Example" to >> continue sampling. >> > > #Don't mess up the original > >> There is probably an easy and faster way to do this. >> Could anybody help me? >> Thanks > -- David Winsemius, MD Heritage Laboratories West Hartford, CT ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.