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

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


Just want to clarify - I know how to do it using base R. I just want
to figure out how to do it in dplyr. This i what I want to achieve:

myvars <- c("x","y","z")
aggregate(md[myvars], by = md[c("device1","device2")], mean, na.rm = T)

Thank you!

On Thu, Jun 25, 2015 at 4:25 PM, Dimitri Liakhovitski
<dimitri.liakhovitski at gmail.com> 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.
> 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



-- 
Dimitri Liakhovitski



More information about the R-help mailing list