【发布时间】:2022-01-14 11:28:02
【问题描述】:
我有一个这样的数据集:
id = rep(paste("id",1:7,sep=""),c(5,2,4,2,3,1,4))
county=rep(c("A","B","C","B","A","D","C"),c(5,2,4,2,3,1,4))
year =c(2011:2015,2012:2013,2011:2014,2013:2014,2011:2013,2014,2012:2015)
age= sample(30:50,size=length(id),replace=T)
race=sample(c("white","Black","Other"),size=length(id),replace=T)
df=data.frame(id=id,county=county,year=year,age=age,race=race)
id county year age race
1 id1 A 2011 32 white
2 id1 A 2012 48 Black
3 id1 A 2013 50 Other
4 id1 A 2014 37 white
5 id1 A 2015 32 white
6 id2 B 2012 48 Black
7 id2 B 2013 48 Other
8 id3 C 2011 40 Other
9 id3 C 2012 33 Other
10 id3 C 2013 42 white
11 id3 C 2014 33 Other
12 id4 B 2013 43 Other
13 id4 B 2014 33 Black
14 id5 A 2011 50 Black
15 id5 A 2012 43 Other
16 id5 A 2013 41 white
17 id6 D 2014 37 Black
18 id7 C 2012 32 white
19 id7 C 2013 31 Other
20 id7 C 2014 34 Other
21 id7 C 2015 35 Other
我想按县、年分组并获得平均年龄和种族类别的百分比。 对于年龄,它可以通过
library(dplyr)
df %>% group_by(county,year) %>% summarise(avgage=mean(age))
但是如何按组获得 3 类种族的百分比?
TIA!
【问题讨论】: