dplyr:传递列名以汇总内部函数

时间:2018-03-07 13:45:34

标签: r dataframe dplyr

我有以下示例,其中我将一个简单的数据框传递给一个汇总列的函数。摘要列的名称s,我希望将其作为函数的参数:

df <- data.frame(id = c(1,1,1,1,1,2,2,2,2,2),
                 a=c(1:10),
                 b=c(10:19))

sum <- function(df, s){
  df <- df %>% 
    group_by(id) %>%
    summarize(s = sum(a))
  return(df)
}

sum(df = df, s = "summarizing.column.label")

但是,无论我设置的值如何,汇总列始终都会使用相同的名称s。有没有办法改变它?

编辑:我想要的输出是:

sum(df = df, s = "summarizing.column.label")

         id     summarizing.column.label
      <dbl> <int>
    1  1.00    15
    2  2.00    40

sum(df = df, s = "a")

         id     a
      <dbl> <int>
    1  1.00    15
    2  2.00    40

2 个答案:

答案 0 :(得分:3)

如果我们传递一个带引号的参数,那么一个选项就在summarise之后,我们使用rename_at

sumf <- function(df, s){

df %>% 
    group_by(id) %>%
    summarize(a = sum(a))%>%
     rename_at("a", ~ s) 


 }

sumf(df, s  ="summarizing.column.label" )
# A tibble: 2 x 2
#     id summarizing.column.label
#  <dbl>                    <int>
#1  1.00                       15
#2  2.00                       40

sumf(df, s  ="a" )
# A tibble: 2 x 2
#     id     a
#   <dbl> <int>
#1  1.00    15
#2  2.00    40

或另一种选择是将:=!!

一起使用
sumf <- function(df, s){

  df %>% 
     group_by(id) %>%
     summarize(a = sum(a))%>%
     rename(!! (s) := a)
}

sumf(df, s  ="summarizing.column.label" )
# A tibble: 2 x 2
#     id summarizing.column.label
#  <dbl>                    <int>
#1  1.00                       15
#2  2.00                       40

summarise

sumf <- function(df, s){

 df %>% 
   group_by(id) %>%
    summarise(!!(s) :=  sum(a))
}

sumf(df,s =&#34; summarizing.column.label&#34;)

答案 1 :(得分:0)

试试这个:

sum <- function(df, s){
  df <- df %>% 
    group_by(id) %>%
    summarize(!!s := sum(a))
  return(df)
}