AmagicalFishy AmagicalFishy - 4 months ago 9
R Question

Split data into 3 ordered chunks of as-close-to equal size as possible

I would like to split this into three chunks whose size is as equal as possible, in such a way that earlier chunks have more entries than later chunks if equality is not possible.

Say I have something like:

x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 , 15, 16, 17, 18, 19)


I'd like:

[1]
1, 2, 3, 4, 5, 6, 7

[2]
8, 9, 10, 11, 12, 13

[3]
14, 15, 16, 17, 18, 19


I can achieve this by defining my own function, using the length of
x
modulo 3, and the floor of the length of
x
divided by 3 to determine the size of each chunk, but is there some way to achieve this in
split
or some such thing that's already defined?

Answer

Use seq_along to generate a vector of indices and use module division to divide it into three chunks, sort the indices and split on the sorted indices give the expected result:

split(x, sort((seq_along(x) - 1)%%3))  # -1 here to convert it to zero based indices so that 
                                       # extra elements will always stack from earlier chunks

# $`0`
# [1] 1 2 3 4 5 6 7

# $`1`
# [1]  8  9 10 11 12 13

# $`2`
# [1] 14 15 16 17 18 19
Comments