Add more arguments to generalize in dplyr

my question is besides defining a function outside sumise_each with multiple arguments, is there any other way to add an argument directly to summaryise_each?

For example, I want to get the average without NAs.this working

mean_fun=function(x)mean(x,na.rm=TRUE) AA_group=AA_new %>% group_by(tractID) AA_group %>% summarise_each(funs(mean_fun)) 

I am wondering if there is a way to add na.rm=TRUE directly to summarise_each , like the more_args option?

and also if I put mean_fun directly on summaryise, namely

 AA_group %>% summarise_each(funs(function(x)mean(x,na.rm=TRUE))) 

and mistake

 expecting a single value 

Does this mean that every time we want to use summaryise_each, we have to define a function outside of this?

+6
source share
1 answer

I guess what you are looking for . as described in ?funs .

Here is a small example using the "iris" dataset, but adding some NA values ​​to it.

 iris2 <- iris set.seed(1) iris2[-5] <- lapply(iris2[-5], function(x) { x[sample(length(x), sample(10, 1))] <- NA x }) 

Now compare the following:

 iris2 %>% group_by(Species) %>% summarise_each(funs(mean)) # Source: local data frame [3 x 5] # # Species Sepal.Length Sepal.Width Petal.Length Petal.Width # 1 setosa 5.006 3.428 NA NA # 2 versicolor NA NA NA NA # 3 virginica NA NA NA NA iris2 %>% group_by(Species) %>% summarise_each(funs(mean_fun)) # Source: local data frame [3 x 5] # # Species Sepal.Length Sepal.Width Petal.Length Petal.Width # 1 setosa 5.006000 3.428000 1.455319 0.2468085 # 2 versicolor 5.939583 2.767347 4.256250 1.3208333 # 3 virginica 6.597959 2.979167 5.547917 2.0191489 iris2 %>% group_by(Species) %>% summarise_each(funs(mean(., na.rm = TRUE))) # Source: local data frame [3 x 5] # # Species Sepal.Length Sepal.Width Petal.Length Petal.Width # 1 setosa 5.006000 3.428000 1.455319 0.2468085 # 2 versicolor 5.939583 2.767347 4.256250 1.3208333 # 3 virginica 6.597959 2.979167 5.547917 2.0191489 
+12
source

Source: https://habr.com/ru/post/975952/


All Articles