![](/img/trans.png)
[英]how to calculate proportion by another variable (not by frequency) in dplyr in R
[英]R: How to use group_by() to calculate the proportion of a variable within another variable?
我想使用 group_by 來顯示每個教育程度(變量: edattain
)在變量: sex
的比例。
到目前為止,我已經計算了以下edattain
的比例:
india04_new4 %>%
mutate(tot = sum(perwt, na.rm = T)) %>%
group_by(edattain) %>%
summarise(share = sum(perwt, na.rm = T)/first(tot))
如何顯示變量中的比例: sex
?
嘗試以下操作:
result <- india04_new4 %>%
group_by(sex, edattain) %>%
summarise(share = sum(perwt, na.rm = TRUE)) %>%
mutate(share = share/sum(share))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.