R-如何将年龄值分类为年龄组

时间:2019-06-18 04:13:54

标签: r

嗨,我在R中创建了一组年龄段:

labs <- c(paste(0, "", sep=""), paste(1,9, sep="-"),paste(seq(10, 80, by = 10), seq(20-1, 90-1, by =10), sep="-", paste(90, "+", sep=""))

out "0" "1-9" "10-19" "20-29" "30-39" "40-49" "50-59" "60-69" "70-79" "80-89" "90+"

如何在实验室中将df的年龄列分类为适当的年龄组?如何使用剪切功能?

预期输出为:

Age AgeGroup
5   1-9
0   0
15  10-19
69  70-79
100 90+

1 个答案:

答案 0 :(得分:1)

# Set seed for reproducibility of results since I use sample() function 
# to generate values for Age variable
set.seed(12345)

#create a numeric variable Age       
Age <- sample(0:110, 100, replace = TRUE)


# Use cut() function to associate each value with a specific age group
AgeGroup <- cut(Age, 
                right=FALSE, 
                breaks = c(0,1,(1:9)*10,1000),
                labels = c("0","1-9",
                           paste((1:8)*10,"-",(1:8 + 1)*10 -1),"90+"))

# create a data frame (if necessary)
df <- data.frame(Age, AgeGroup)
head(df)

# 1  80  80 - 89
# 2  97      90+
# 3  84  80 - 89
# 4  98      90+
# 5  50  50 - 59
# 6  18  10 - 19