我在dplyr中使用了group_by函数,但是,在我正在分组的变量中,有NAs,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()中的参数,但是没有用 .
1 回答
在开始分析之前过滤掉
NA
:并继续 .