Re: [R] Splitting a vector into equal groups
utkarshsinghal wrote: Hi All, I have vector of length 52, say, x=sample(30,52,replace=T). I want to sort x and split into five *nearly equal groups*. Note that the observations are repeated in x so in case of a tie I want both the observations to fall in same group. This seems a very common task to do, but still I couldn't find an R function to do this. Any help would be highly appreciated. See ?cut for groups equal in in its range or ?co.intervals in package lattice for intervals somewhat equal in number of observations. Uwe Ligges Regards Utkarsh [[alternative HTML version deleted]] __ 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. __ 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.
Re: [R] Splitting a vector into equal groups
G'day Utkarsh, On Mon, 04 May 2009 11:51:21 +0530 utkarshsinghal wrote: > I have vector of length 52, say, x=sample(30,52,replace=T). I want to > sort x and split into five *nearly equal groups*. What do you mean by *nearly equal groups*? The size of the groups should be nearly equal? The sum of the elements of the groups should be nearly equal? > Note that the observations are repeated in x so in case of a tie I > want both the observations to fall in same group. Then it becomes even more important to define what you mean with "nearly equal groups". As a start, you may consider: R> set.seed(1) R> x=sample(30,52,replace=T) R> xrle <- rle(sort(x)) R> xrle Run Length Encoding lengths: int [1:25] 2 1 2 2 3 1 1 1 5 1 ... values : int [1:25] 1 2 4 6 7 8 9 11 12 13 ... R> cumsum(xrle$lengths) [1] 2 3 5 7 10 11 12 13 18 19 24 25 26 28 29 32 35 38 [19] 43 45 46 48 49 51 52 and use this to determine our cut-offs. E.g., should the first group have 10, 11 or 12 elements in this case? The information in xrle should enable you to construct your five groups once you have decided on a grouping. HTH. Cheers, Berwin === Full address = Berwin A TurlachTel.: +65 6516 4416 (secr) Dept of Statistics and Applied Probability+65 6516 6650 (self) Faculty of Science FAX : +65 6872 3919 National University of Singapore 6 Science Drive 2, Blk S16, Level 7 e-mail: sta...@nus.edu.sg Singapore 117546http://www.stat.nus.edu.sg/~statba __ 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.
Re: [R] Splitting a vector into equal groups
check functions cut() and quantile(), and cut2() from package Hmisc; maybe the following is close to what you want: x <- sample(30, 52, replace = TRUE) k <- 5 # how many groups qs <- quantile(x, seq(0, 1, length.out = k + 1)) y <- cut(x, round(qs), include.lowest = TRUE) y table(y) I hope it helps. Best, Dimitris utkarshsinghal wrote: Hi All, I have vector of length 52, say, x=sample(30,52,replace=T). I want to sort x and split into five *nearly equal groups*. Note that the observations are repeated in x so in case of a tie I want both the observations to fall in same group. This seems a very common task to do, but still I couldn't find an R function to do this. Any help would be highly appreciated. Regards Utkarsh [[alternative HTML version deleted]] __ 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. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 __ 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.
Re: [R] Splitting a vector into equal groups
lattice:::equal.count may be what you want. 2009/5/4 utkarshsinghal : > Hi All, > > I have vector of length 52, say, x=sample(30,52,replace=T). I want to > sort x and split into five *nearly equal groups*. Note that the > observations are repeated in x so in case of a tie I want both the > observations to fall in same group. > This seems a very common task to do, but still I couldn't find an R > function to do this. Any help would be highly appreciated. > > Regards > Utkarsh > > > > [[alternative HTML version deleted]] > > __ > 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. > -- HUANG Ronggui, Wincent PhD Candidate Dept of Public and Social Administration City University of Hong Kong Home page: http://asrr.r-forge.r-project.org/rghuang.html __ 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.
[R] Splitting a vector into equal groups
Hi All, I have vector of length 52, say, x=sample(30,52,replace=T). I want to sort x and split into five *nearly equal groups*. Note that the observations are repeated in x so in case of a tie I want both the observations to fall in same group. This seems a very common task to do, but still I couldn't find an R function to do this. Any help would be highly appreciated. Regards Utkarsh [[alternative HTML version deleted]] __ 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.