dplyr summarise_的非标准评估导致不同的结果

时间:2015-09-01 20:36:55

标签: r dplyr

我希望在我的函数中调用summarise_ dplyr个包,这是我尝试过但我对meanmedian函数有所不同,是什么' s我的方法有问题吗?

library(dplyr)
df <- data.frame(A=c(1,2,3))
getMean <- function(df, col) {
  col <- as.symbol(col)
  df %>%
    summarise_(Mean = ~mean(col))
}

getMedian <- function(df, col) {
  col <- as.symbol(col)
  df %>%
    summarise_(Median = ~median(col))
}

getMean(df, 'A')
   Mean
1    2

getMedian(df, 'A')
Error: object 'A' not found 

1 个答案:

答案 0 :(得分:4)

We can use lazyeval

library(lazyeval)
library(dplyr)
getMedian <- function(df, col) {
          df %>%
           summarise_(.dots= list(Median=interp(~median(v), v= as.name(col))))
    }

getMedian(df, 'A')
#  Median
#1      2

We could use a single function to do the mean, median etc. by using the function name as argument.

getFun <- function(df, col, func) {
      FUN <- match.fun(func)
      nm1 <- sub('^(.)', '\\U\\1', substitute(func), perl=TRUE)
      df %>%
        summarise_(interp(~FUN(v), v= as.name(col)))%>%
        setNames(., nm1)
}

getFun(df, 'A', median)
#  Median
#1      2
getFun(df, 'A', mean)
#  Mean
#1    2

getFun(df, 'A', var)
#  Var
#1   1

getFun(df, 'A', min)
#  Min
#1   1
getFun(df, 'A', max)
#  Max
#1   3