[R] dplyr, summarize_each, mean - dealing with NAs

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Thu Jun 25 22:25:37 CEST 2015


Hello!

I have a data frame md:

    md <- data.frame(x = c(3,5,4,5,3,5), y = c(5,5,5,4,4,1), z = c(1,3,4,3,5,5),
          device1 = c("c","a","a","b","c","c"), device2 =
c("B","A","A","A","B","B"))
    md[2,3] <- NA
    md[4,1] <- NA
    md

I want to calculate means by device1 / device2 combinations using dplyr:

    library(dplyr)
    md %>% group_by(device1, device2) %>% summarise_each(funs(mean))

However, I am getting some NAs. I want the NAs to be ignored (na.rm =
TRUE) - I tried, but the function doesn't want to accept this
argument.
Both these lines result in error:

    md %>% group_by(device1, device2) %>% summarise_each(funs(mean),
na.rm = TRUE)
    md %>% group_by(device1, device2) %>% summarise_each(funs(mean,
na.rm = TRUE))

Thank you for your advice!


-- 
Dimitri Liakhovitski



More information about the R-help mailing list