Witold,

Here's one way:

Vec <- rnorm(30)
Vec.cut <- cut(Vec, breaks=c(quantile(Vec, probs = seq(0, 1, by = 0.20))),
labels=c("0-20","20-40","40-60","60-80","80-100"), include.lowest=TRUE)
table(Vec.cut)


or determine the breaks automatically:

cut.size <- function(x, size) {
  cut.prob <- size/length(x)
if (length(x)%%size != 0) warning("Equal sized groups only possible by dropping some elements from x") Vec.cut <- cut(x, breaks=c(quantile(x, probs = seq(0, 1, by = size/length(x)))), include.lowest=TRUE)
}
CUT <- cut.size(Vec, 6)
table(CUT)


Of course, when asking for
cut.size(Vec, 7)
this will yield 4 equal-sized groups of 7, because there is no way to perfectly split 30 observations in groups of 7 each (the highest two values from Vec will be dropped).

HTH,
Peter



Op 18-7-2013 0:02, Greg Snow schreef:
You could use the quantile function to choose the cut points and pass that
to cut.

Or you could sort the vector and just take the first n elements as the 1st
group, etc.


On Wed, Jul 17, 2013 at 3:43 PM, Witold E Wolski <wewol...@gmail.com> wrote:

I would like to "cut" a vector into groups of equal nr of elements.
looking for a function on the lines of cut but where I can specify
the size of the groups instead of the nr of groups.




--
Witold Eryk Wolski

______________________________________________
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.

Reply via email to