在我的论文中,我有一个带有变量year
,agegroup
,count
的数据帧。对于每个year
,我想将agegroups
'0-4','5-9‘和'10-14’折叠成'0-14',对count
求和。
我想要的是:
year agegroup count
1 1990 0-4 1
2 1990 5-9 1
3 1990 10-14 1
4 1990 15-19 7
5 1991 0-4 1
6 1991 5-9 3
7 1991 10-14 1
8 1991 15-19 7
变成这样:
year agegroup count
1 1990 0-14 3
2 1990 15-19 7
3 1991 0-14 5
4 1991 15-19 7
我想不出如何干净利落地做这件事,希望能有任何建议!
发布于 2021-04-15 19:31:43
df$agegroup2=gsub("0-4|5-9|10-14","0-14",df$agegroup)
aggregate(count~year+agegroup2,df,sum)
year agegroup2 count
1 1990 0-14 3
2 1991 0-14 5
3 1990 15-19 7
4 1991 15-19 7
https://stackoverflow.com/questions/67107596
复制相似问题