5

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. 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.

Mike Edinger
  • 175
  • 1
  • 4
  • 11
  • 3
    Perhaps you could just filter out all NA entries in the grouping variable: `data %>% filter(!is.na(DMDEDUC2)) %>% group_by(DMDEDUC2) %>% ...` – talat Apr 07 '15 at 20:59

2 Answers2

3

Filter out the NAs before beginning your analyses:

data<-data[!is.na(DMDEDUC2),]

and continue on.

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
jeremycg
  • 24,657
  • 5
  • 63
  • 74
0
  library(tidyverse)

or

 library(dplyr)

then

data %>%
  filter( is.na(DMDEDUC2) == FALSE) %>% 
    group_by (DMDEDUC2) %>% 
       ed.prop()

or as as suggested by talat

data %>% 
  filter(!is.na(DMDEDUC2)) %>% 
     group_by(DMDEDUC2) %>%  
         ed.prop()

*The working of ed.prop () function is not verified

Adam
  • 434
  • 1
  • 4
  • 18