简体   繁体   中英

Grouping Data in R with a new Column Name

Sample data

Name: test
id   age
1    25
2    48
3    77
4    17
5    27

I wanted to add a new column called age_group so I added:

test$age_group<-NA

In this new column I wanted to add a group (obviously) by age.

<18=1
19-30=2 
31-45=3
46-60=4
61-75=5
>76 = 6

so the data would now look like this:

id   age  age_group
1    25   2
2    48   4
3    77   6
4    17   1
5    27   2

Can someone help with the code on how to populate age_group ?

Try this:

df$age_group <- cut(df$age, breaks = c(0,18,30,45,60,75,Inf), labels = 1:6)

#  id age age_group
#1  1  25         2
#2  2  48         4
#3  3  77         6
#4  4  17         1
#5  5  27         2

Here is another efficient option with findInterval

findInterval(test$age, c(0, 18, 30,45, 60, 75, Inf))
#[1] 2 4 6 1 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM