[英]how to calculate proportion by another variable (not by frequency) in dplyr in R
Using mtcars data, I want to calculate proportion of mpg for each group of cyl and am. 使用mtcars数据,我想计算每组cyl和am的mpg比例。 How to calc it?
如何计算呢?
mtcars %>%
group_by(cyl, am) %>%
summarise(mpg = n(mpg)) %>%
mutate(mpg.gr = mpg/(sum(mpg))
Thanks in advance! 提前致谢!
If I understand you correctly, you want the proportion of records for each combination of cyl
and am
. 如果我对您的理解正确,那么您想要
cyl
和am
每个组合的记录所占的比例。 If so, then I believe your code isn't working because n()
doesn't accept an argument. 如果是这样,那么我认为您的代码无法正常工作,因为
n()
不接受参数。 You also need to ungroup()
before calculating your proportions. 您还需要先
ungroup()
然后再计算比例。
You could simply do: 您可以简单地执行以下操作:
mtcars %>%
group_by(cyl, am) %>%
summarise(mpg = n()) %>%
ungroup() %>%
mutate(mpg.gr = mpg/(sum(mpg))
#> # A tibble: 6 x 4
#> cyl am mpg mpg.gr
#> <dbl> <dbl> <int> <dbl>
#> 1 4 0 3 0.0938
#> 2 4 1 8 0.25
#> 3 6 0 4 0.125
#> 4 6 1 3 0.0938
#> 5 8 0 12 0.375
#> 6 8 1 2 0.0625
Note that thanks to ungroup()
, the proportions are calculated using the counts of all records, not just those within the cyl
group, as before. 请注意,由于使用了
ungroup()
,比例是使用所有记录的计数来计算的,而不是像以前那样仅使用cyl
组中的记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.