year name sex numberOfBirth
1950 mark M 25
1950 jill F 60
1950 jesy F 26
1950 john M 50
1950 ana F 78
.
.
.
2010 tom M 67
2010 jack M 25
2010 lia F 45
2010 jesse F 36
2000行
答案 0 :(得分:3)
或使用data.table
library(data.table)
setDT(df)[, list(Males = sum(sex == "M")/.N,
Females = sum(sex == "F")/.N), by = year]
或@ user20650提出的基础R解决方案
prop.table(with(df, table(year, sex)), 1)
答案 1 :(得分:2)
library(dplyr)
df %>% group_by(year) %>%
summarize(pct.males = sum(df$sex == 'M') / length(df$sex) * 100,
pct.female = sum(df$sex == 'F') / length(df$sex) * 100)