Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dplyr summarise_each with na.rm

Tags:

r

dplyr

Is there a way to instruct dplyr to use summarise_each with na.rm=TRUE? I would like to take the mean of variables with summarise_each("mean") but I don't know how to specify it to ignore missing values.

like image 517
paljenczy Avatar asked Sep 10 '14 07:09

paljenczy


People also ask

What does Summarise_each do in R?

summarise_each: Summarise and mutate multiple columns. Apply one or more functions to one or more columns. Grouping variables are always excluded from modification.

What does Na Rm mean in R?

When using a dataframe function na. rm in r refers to the logical parameter that tells the function whether or not to remove NA values from the calculation. It literally means NA remove. It is neither a function nor an operation. It is simply a parameter used by several dataframe functions.

Why mean is na in R?

However why did your mean return NA? When performing mathematical operations on numbers in R , most functions will return the value NA if the data you are working with include missing or nodata values. Returning NA values allows you to see that you have missing data in your dataset.


1 Answers

Following the links in the doc, it seems you can use funs(mean(., na.rm = TRUE)):

library(dplyr) by_species <- iris %>% group_by(Species) by_species %>% summarise_each(funs(mean(., na.rm = TRUE))) 
like image 184
flodel Avatar answered Sep 28 '22 10:09

flodel