繁体   English   中英

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

[英]R - How to sort age values into age group

嗨,我在 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+
# 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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