[英]R: calculate proportion of a factor combined with group by in a data.frame
我想使用 group by 在数据帧上使用summarise
进行多次计算。 输入数据:
dat <- data.frame (ID = c(1:10),
var1 = as.factor(c("A","B","A","A","B","B","B","C","A","B")),
Var2 = as.factor(c("low","medium","low","low","medium","high","high","high","high","high")))
现在我想对 var1 进行分组,计算 ID 并计算 var2 = high 的比例。 我的输出应该是这样的:
var1 total prop_high
1 A 4 0.25
2 B 5 0.60
3 C 1 1.00
到目前为止,我得到了以下代码,但我陷入了比例计算
dat2 <- dat %>%
group_by(var1) %>%
summarise(total = n(),
prop_high = )
你可以把mean
逻辑值的获取比例。
library(dplyr)
dat %>%
group_by(var1) %>%
summarise(total = n(),
prop_high = mean(Var2 == 'high'))
#Same as
#prop_high = sum(Var2 == 'high')/n())
# var1 total prop_high
# <fct> <int> <dbl>
#1 A 4 0.25
#2 B 5 0.6
#3 C 1 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.