Skip to content

cut into groups of equal nr of elements...

12 messages · Witold E Wolski, arun, Wensui Liu +3 more

#
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
#
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.
#
Sorry, there was a mistake:
fun1 should be:
fun1<- function(x,nr) {((seq_along(x)-1)%/%nr)+1}

vec3<- c(4,5,7,9,8,5)
?fun1(vec3,2)
#[1] 1 1 2 2 3 3

split(vec3,fun1(vec3,2))


A.K.



----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: Witold E Wolski <wewolski at gmail.com>
Cc: R help <r-help at r-project.org>
Sent: Wednesday, July 17, 2013 6:04 PM
Subject: Re: [R] cut into groups of equal nr of elements...

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.
#
On Jul 17, 2013, at 4:43 PM, Witold E Wolski <wewolski at gmail.com> wrote:

            
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)
[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
$`(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
#
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)

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.

HTH,
Peter


Op 18-7-2013 18:09, Marc Schwartz schreef:
1 day later
#
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: