[R] cut into groups of equal nr of elements...
Marc Schwartz
marc_schwartz at me.com
Thu Jul 18 01:04:30 CEST 2013
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
More information about the R-help
mailing list