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
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 <wewolski@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. >-- Gregory (Greg) L. Snow Ph.D. 538280@gmail.com [[alternative HTML version deleted]]
HI, Not sure whether this is what you wanted. ?vec1<- 1:7 ?fun1<- function(x,nr) {((x-1)%/%nr)+1} ?fun1(vec1,2) #[1] 1 1 2 2 3 3 4 ?fun1(vec1,3) #[1] 1 1 1 2 2 2 3 split(vec1,fun1(vec1,2)) A.K. ----- Original Message ----- From: Witold E Wolski <wewolski at gmail.com> To: r-help at r-project.org Cc: Sent: Wednesday, July 17, 2013 5:43 PM Subject: [R] cut into groups of equal nr of elements... 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 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.
?qcut On Jul 17, 2013 5:45 PM, "Witold E Wolski" <wewolski@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. >[[alternative HTML version deleted]]
On Jul 17, 2013, at 4:43 PM, Witold E Wolski <wewolski at 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.In addition to the other options, if the 'breaks' argument to cut() is a single number, rather than a vector of cut points, it defines the number of intervals to break the 'x' vector into, which of course you can derive from length(x) / size. Thus: set.seed(1) Vec <- sample(30)> Vec[1] 8 11 17 25 6 23 27 16 14 2 5 4 13 7 18 30 29 24 20 9 10 21 [23] 26 1 22 15 28 12 3 19 # Split into 5 groups of 6 each> split(Vec, cut(Vec, 5))$`(0.971,6.78]` [1] 6 2 5 4 1 3 $`(6.78,12.6]` [1] 8 11 7 9 10 12 $`(12.6,18.4]` [1] 17 16 14 13 18 15 $`(18.4,24.2]` [1] 23 24 20 21 22 19 $`(24.2,30]` [1] 25 27 30 29 26 28 Regards, Marc Schwartz