[R] Splitting a vector into equal groups
Dimitris Rizopoulos
d.rizopoulos at erasmusmc.nl
Mon May 4 09:06:09 CEST 2009
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 at 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
More information about the R-help
mailing list