【发布时间】:2015-04-07 20:57:37
【问题描述】:
我在 dplyr 中使用 group_by 函数,但是,在我分组的变量中,有 NA,group_by 将其组成一个单独的组。例如,我正在使用以下具有输出的代码:
> education <- group_by(data, DMDEDUC2)
> ed.prop <- summarise(education,
+ total = n(),
+ num.obese = sum(as.numeric(is.obese)),
+ pbar = num.obese/total,
+ margin = qnorm(.975)*sqrt(pbar*(1-pbar)/total),
+ lower = pbar - margin,
+ upper = pbar + margin
+ )
> ed.prop <- select(ed.prop, education = DMDEDUC2, total, num.obese, pbar, lower, upper)
> ed.prop
Source: local data frame [6 x 6]
education total num.obese pbar lower upper
1 1 501 170 0.3393214 0.2978613 0.3807814
2 2 734 297 0.4046322 0.3691244 0.4401399
3 3 1098 448 0.4080146 0.3789449 0.4370843
4 4 1576 605 0.3838832 0.3598728 0.4078937
5 5 1324 353 0.2666163 0.2427979 0.2904347
6 NA 4 0 0.0000000 0.0000000 0.0000000
如何才能不生成最后一行?我已经尝试将 na.rm = TRUE 作为 group_by() 中的参数,但没有奏效。
【问题讨论】:
-
也许您可以过滤掉分组变量中的所有 NA 条目:
data %>% filter(!is.na(DMDEDUC2)) %>% group_by(DMDEDUC2) %>% ...