使用dplyr将列名作为参数传递给函数 [英] Passing column name as parameter to a function using dplyr

查看:64
本文介绍了使用dplyr将列名作为参数传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下数据框:

transid<-c(1,2,3,4,5,6,7,8)
accountid<-c(a,a,b,a,b,b,a,b)
month<-c(1,1,1,2,2,3,3,3)
amount<-c(10,20,30,40,50,60,70,80)
transactions<-data.frame(transid,accountid,month,amount)

我正在尝试使用dplyr软件包动词编写每个帐户id的每月总金额的函数.

I am trying to write function for total monthly amount for each accountid using dplyr package verbs.

my_sum<-function(df,col1,col2,col3){
df %>% group_by_(col1,col2) %>%summarise_(total_sum = sum(col3))
}

my_sum(transactions, "accountid","month","amount")

要获得如下结果:

accountid   month  total_sum
a            1       30
a            2       40
a            3       70
b            1       30
b            2       50
b            3       140

我收到如下错误:-sum(col3)中的错误:参数的'类型'(字符)无效.如何在摘要函数中将列名作为参数传递而没有引号?

I am getting error like:- Error in sum(col3) : invalid 'type' (character) of argument.How to pass column name as parameter without quote in summarise function?

推荐答案

我建议以下解决方案:

my_sum <- function(df, col_to_sum,...) {

    col_to_sum <- enquo(col_to_sum)
    group_by <- quos(...)

    df %>%
        group_by(!!!group_by) %>%
        summarise(total_sum = sum(!!col_to_sum)) %>% 
        ungroup()
}

transactions %>% my_sum(amount, accountid, month)

结果

>> transactions %>% my_sum(amount, accountid, month)
# A tibble: 6 x 3
  accountid month total_sum
     <fctr> <dbl>     <dbl>
1         a     1        30
2         a     2        40
3         a     3        70
4         b     1        30
5         b     2        50
6         b     3       140

数据

在您最初的答案中,您传递了未加注释的字符串,我已经解决了使用 Hmisc:Cs 函数,但是原则上,您应该将字符串用" 括起来;除非您当然要调用某些名为 a b 等的对象.从最初的问题还不清楚.

Data

In you original answer you have passed unqoted strings, I've solved that using Hmisc:Cs function but, on principle, you should surround your strings with ""; unless, of course, you are calling some objects named a, b and so forth. It wasn't clear from the original question.

使用的数据:

transid <- c(1, 2, 3, 4, 5, 6, 7, 8)
accountid <- Hmisc::Cs(a, a, b, a, b, b, a, b)
month <- c(1, 1, 1, 2, 2, 3, 3, 3)
amount <- c(10, 20, 30, 40, 50, 60, 70, 80)
transactions <- data.frame(transid, accountid, month, amount)

注释

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆