Group data based on intervals and assign group to new column
You may use cut
and create groups based on defined intervals.
transform(DF, GROUP = cut(AGE, c(0, seq(19, max(AGE) + 10, 10)), labels = FALSE))
# NAME AGE GROUP
#1 Gait 33 3
#2 Roc 43 4
#3 Bo 37 3
#4 Hernd 45 4
#5 Bet 44 4
#6 Oln 35 3
#7 Gai 22 2
#8 Rock 30 3
#9 Mil 38 3
#10 Arli 23 2
#11 Re 45 4
#12 Fred 43 4
#13 Ro 67 6
#14 Rock 43 4
#15 Wheat 28 2
#16 Germa 47 4
#17 Rock 16 1
#18 Nort 29 2
#19 Arli 22 2
#20 Rockv 31 3
The key part here is how we create intervals with c
and seq
which define the groups.
c(0, seq(19, max(DF$AGE) + 10, 10))
#[1] 0 19 29 39 49 59 69