luffe luffe - 2 months ago 15
R Question

Group sequence of integers

I have bunch of observations

x = c(1, 2, 4, 1, 6, 7, 11, 11, 12, 13, 14)


that I want to turn into the group:

y = c(1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 3)


I.e I want the first
5
integers (1 to 5) to constitute one group, the next
5
integers to constitute the next group (6 to 10), and so on.

Is there a straightforward way to accomplish this without a loop?

Clarification: I need to programmatically create the groups form the input vector (
x
)

Answer

We can use %/% to create the group

x%/%5+1
#[1] 1 1 1 1 2 2 3 3 3 3 3