dplyr字符串作为列引用 [英] dplyr string as column reference

查看:104
本文介绍了dplyr字符串作为列引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这里有一个例子 - 分组数据集和一个简单的函数,我试图传递一个字符串作为列的引用。谢谢!

 机器<  -  data.frame(Date = c(1/31/2014,1/31 / 2014,2/28/2014,2/28/2014,3/31/2014,3/31/2014),
Model.Num = c(123 ,456,123,456,123,456),
成本= c(200,300,250,350,300,400))

my.fun< - function(data,colname){
mutate(data,position = cumsum(as.name(colname)))
}

机器& - 机器%>%group_by(Date,Model.Num)
机器< - my.fun(机器,成本)


解决方案

这是一个选项,使用 interp() lazyeval 软件包,它与您的 dplyr 安装一起提供。在您的功能内,您将需要使用标准评估版本的> dplyr 功能。在这种情况下,这将是 mutate _()



请注意,新列位置由于您在机器中设置分组,将与此成本列相同。第二次调用 my_fun()显示它在另一组分组变量上工作。

 code> library(dplyr)
库(lazyeval)

my_fun< - function(data,col){
mutate_(data,position = interp (x),x = as.name(col)))
}

my_fun(机器,成本)
#日期Model.Num成本位置
#1 1/31/2014 123 200 200
#2 1/31/2014 456 300 300
#3 2/28/2014 123 250 250
#4 2/28/2014 456 350 350
#5 3/31/2014 123 300 300
#6 3/31/2014 456 400 400

##第二个例子 - 不同的分组
my_fun (group_by(机器,Model.Num),Cost)
#日期Model.Num成本位置
#1 1/31/2014 123 200 200
#2 1/31/2014 456 300 300
#3 2/28/2014 123 250 450
#4 2/28/2014 456 350 650
#5 3/31/2014 123 300 750
# 6 3/31 / 2014 456 400 1050


Is there anyway to pass a string as column reference to a dplyr procedure?

Here is an example - with a grouped dataset and a simple function where I try to pass a string as reference to a column. Thanks!

machines <- data.frame(Date=c("1/31/2014", "1/31/2014", "2/28/2014", "2/28/2014", "3/31/2014", "3/31/2014"), 
            Model.Num=c("123", "456", "123", "456", "123", "456"), 
            Cost=c(200, 300, 250, 350, 300, 400))

my.fun <- function(data, colname){
    mutate(data, position=cumsum(as.name(colname)))
}

machines <- machines %>% group_by(Date, Model.Num)     
machines <- my.fun(machines, "Cost")

解决方案

Here's an option that uses interp() from the lazyeval package, which came with your dplyr install. Inside your function(s), you'll need to use the standard evaluation version of the dplyr functions. In this case that would be mutate_().

Note that the new column position will be identical to the Cost column here because of how you've set up the grouping in machines. The second call to my_fun() shows it working on a different set of grouping variables.

library(dplyr)
library(lazyeval)

my_fun <- function(data, col) {
    mutate_(data, position = interp(~ cumsum(x), x = as.name(col)))
}

my_fun(machines, "Cost")
#        Date Model.Num Cost position
# 1 1/31/2014       123  200      200
# 2 1/31/2014       456  300      300
# 3 2/28/2014       123  250      250
# 4 2/28/2014       456  350      350
# 5 3/31/2014       123  300      300
# 6 3/31/2014       456  400      400

## second example - different grouping
my_fun(group_by(machines, Model.Num), "Cost")
#        Date Model.Num Cost position
# 1 1/31/2014       123  200      200
# 2 1/31/2014       456  300      300
# 3 2/28/2014       123  250      450
# 4 2/28/2014       456  350      650
# 5 3/31/2014       123  300      750
# 6 3/31/2014       456  400     1050

这篇关于dplyr字符串作为列引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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