0

Using R I am trying to create a grouping variable (y) so that every 10 rows along the whole, uneven dataset are grouped together based on the same number as showed below:

data.frame(x,y)
    x y
1   1 1
2   2 1
3   3 1
4   4 1
5   5 1
6   6 1
7   7 1
8   8 1
9   9 1
10 10 1
11 11 2
12 12 2
13 13 2
14 14 2
15 15 2
16 16 2
17 17 2
18 18 2
19 19 2
20 20 2
21 21 3
22 22 3
23 23 3
24 24 3
25 25 3
26 26 3
27 27 3
28 28 3
29 29 3
30 30 3
31 31 4
32 32 4
33 33 4

I tried with seq() and rep() but no luck so far. Can someone please help? Thanks in advance

Martyna F
  • 55
  • 6

1 Answers1

0

You can create a sequence and use %/% to get groups. If needed you can add one to those numbers.

DF <- data.frame(x=1:33)

DF$y <- 0:(nrow(DF)-1) %/% 10

DF
#    x y
#1   1 0
#2   2 0
#3   3 0
#4   4 0
#5   5 0
#6   6 0
#7   7 0
#8   8 0
#9   9 0
#10 10 0
#11 11 1
#12 12 1
#13 13 1
#14 14 1
#15 15 1
#16 16 1
#17 17 1
#18 18 1
#19 19 1
#20 20 1
#21 21 2
#22 22 2
#23 23 2
#24 24 2
#25 25 2
#26 26 2
#27 27 2
#28 28 2
#29 29 2
#30 30 2
#31 31 3
#32 32 3
#33 33 3
GKi
  • 37,245
  • 2
  • 26
  • 48