Dimitri Liakhovitski
2015-Jun-25 20:25 UTC
[R] dplyr, summarize_each, mean - dealing with NAs
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
2015-Jun-25 20:35 UTC
[R] dplyr, summarize_each, mean - dealing with NAs
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
Dimitri Liakhovitski
2015-Jun-25 20:37 UTC
[R] dplyr, summarize_each, mean - dealing with NAs
I found the answer: md %>% group_by(device1, device2) %>% summarise_each(funs(mean(., na.rm = TRUE))) On Thu, Jun 25, 2015 at 4:35 PM, Dimitri Liakhovitski <dimitri.liakhovitski at gmail.com> wrote:> 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-- Dimitri Liakhovitski
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