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

David Winsemius dwinsemius at comcast.net
Fri Jun 26 00:32:40 CEST 2015


On Jun 25, 2015, at 1:25 PM, Dimitri Liakhovitski wrote:

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


The help page for the `funs`-function has several examples:

This succeeds:

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


> 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
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list