Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add more argument to summarise in dplyr

Tags:

r

dplyr

my question is besides defining a function outside of summarise_each with multiple argument, is there another way to add the argument directly in the summarise_each?

For example I want to get the mean without NAs.this way works

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 whether there is a way to add na.rm=TRUE directly to summarise_each,such as more_args option?

and also if I put mean_fun directly to summarise_each namely,

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

and the error is

expecting a single value

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

like image 470
MYjx Avatar asked Sep 28 '14 03:09

MYjx


1 Answers

I'm guessing you're looking for ., as documented at ?funs.

Here's a small example, using the "iris" dataset, but adding some NA values into 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
like image 113
A5C1D2H2I1M1N2O1R2T1 Avatar answered Oct 11 '22 13:10

A5C1D2H2I1M1N2O1R2T1