【发布时间】:2021-05-19 15:31:31
【问题描述】:
我正在用 dplyr 计算总和,但遇到了这个问题:
library(dplyr)
toto <- data.frame(
classe = c("CP","CP2","CP2","CP2"),
in_flores = c(1,0,1,1),
effectif = c(10,50,20,30),
effectif2 = c(10,50,14,NA)
)
toto %>%
group_by(classe) %>%
summarise(
eff = if_else(in_flores>=1,effectif,0) %>% sum(na.rm = T),
effeff2 = if_else(in_flores>=1,effectif+effectif2,0) %>% sum(na.rm = T)
)
我希望 effeff2 得到 64 而不是 34... 如何处理缺失值? 非常感谢提前
【问题讨论】:
-
使用您的代码,您需要重新定义
+,因为在 R 的算术版本中 30+NA 是 NA。