[英]dplyr: removing NAs from group_by variable
I'm using the group_by function in dplyr, however, in the variable that I'm grouping by, there are NAs, which group_by is making into a seperate group.我在 dplyr 中使用 group_by 函数,但是,在我分组的变量中,有 NAs,group_by 将其组合成一个单独的组。 For example, I'm using the following code that has the output:例如,我正在使用以下具有输出的代码:
> 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
How can I make it so that the last row isn't generated?我怎样才能使它不生成最后一行? I've already tried na.rm = TRUE as an argument in group_by() and that didn't work.我已经在 group_by() 中尝试将 na.rm = TRUE 作为参数,但没有奏效。
Filter out the NA
s before beginning your analyses:在开始分析之前过滤掉NA
:
data<-data[!is.na(DMDEDUC2),]
and continue on.并继续。
library(tidyverse)
or或者
library(dplyr)
then然后
data %>%
filter( is.na(DMDEDUC2) == FALSE) %>%
group_by (DMDEDUC2) %>%
ed.prop()
or as as suggested by talat或者按照 talat 的建议
data %>%
filter(!is.na(DMDEDUC2)) %>%
group_by(DMDEDUC2) %>%
ed.prop()
*The working of ed.prop ()
function is not verified * ed.prop ()
函数的工作没有经过验证
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.