dplyr标准评估:总结_与变量名称相加的变量 [英] dplyr standard evaluation: summarise_ with variable name for summed variable

查看:132
本文介绍了dplyr标准评估:总结_与变量名称相加的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经历了很多类似于我的问题,但只解决了我的一个问题。我正在使用标准评估dplyr来适应变量名称。这对管道中的filter_和group_by_可以正常工作。但是,为了总结,我不能为我的总和的指标变量名。一个例子将会清楚。

 库(dplyr)
库(lazyeval)

#create data
a< - data.frame(
x = c(2010,2010,2011,2011,2011),
y_zm = c(rep(10,5)),
y_r2 = c(rep(20,5)))

#定义变量名称
标签< - 2011
metric< - y
run1< - zm
run2< - r2

#具有固定变量名称的管道的工作示例
a%>%
filter_(〜x == tag)%>%
group_by_(tag)%>%
summarise_(variable_name = interp(〜sum(var,na.rm = T))
var = as.name(paste0(metric,_,run1))))

#我想要做的非工作示例
a%>%
filter_(〜x == tag)%>%
group_by_(tag)%>%
summarise_(as.name(paste0(metric,_,run1))=
interp(〜sum(var,na.rm = T),
var = as.name(paste0(metric,_,run1))))
/ pre>

我试过了很多不同的事情涉及到as.name()或interp(),但似乎没有任何效果。

解决方案

NSE vignette一段时间,戳戳事物,我发现如果您使用,则可以使用中的 setNames $ c> .dots 参数,并将 interp 工作放在列表中。

  a%>%
filter_(〜x == tag)%>%
group_by_(tag)%>%
总结_ = setNames(list(interp(〜sum(var,na.rm = TRUE))
var = as.name(paste0(metric,_,run1)))),
paste0 ,_,run1)))

来源:本地数据框架[1 x 2]

2011 y_zm
1 2011 30

您还可以添加一个重命名_ 步骤来做同样的事情。我可以看到这不太理想,因为它依赖于知道您在 summary _ _ / / c $ c>中使用的名称。但是,如果您始终使用相同的名称,例如 variable_name ,在某些情况下,这似乎是可行的替代方案。

  a%>%
filter_(〜x == tag)%>%
group_by_(tag)%>%
summarise_ = interp(〜sum(var,na.rm = T),
var = as.name(paste0(metric,_,run1))))%>%
重命名_ = setNames(variable_name,paste0(metric,_,run1)))

来源:本地数据框[1 x 2]

2011 y_zm
1 2011 30


I went through a lot of questions that are similar to mine but only addressed one part of my problem. I am using dplyr with standard evaluation to accommodate variable names. This works fine for filter_ and group_by_ in a pipe. However, for summarize I cannot have a variable name for the metric I'm summing. An example will make it clear.

library(dplyr)
library(lazyeval)

# create data
a <- data.frame(
  x = c(2010, 2010, 2011, 2011, 2011),
  y_zm = c(rep(10, 5)),
  y_r2 = c(rep(20, 5)))

# define variable names
tag <- "2011"
metric <- "y"
run1 <- "zm"
run2 <- "r2"

# working example for a pipe with fixed variable name
a %>%
  filter_(~x == tag) %>%
  group_by_(tag) %>%
  summarise_(variable_name = interp(~sum(var, na.rm = T), 
                                    var = as.name(paste0(metric,"_",run1))))

# non-working example of what I want to do
a %>%
  filter_(~x == tag) %>%
  group_by_(tag) %>%
  summarise_(as.name(paste0(metric,"_",run1)) = 
               interp(~sum(var, na.rm = T), 
                      var = as.name(paste0(metric,"_",run1))))

I tried a lot of different things involving as.name() or interp() but nothing seems to work.

解决方案

After poring over the NSE vignette for awhile and poking at things, I found you can use setNames within summarise_ if you use the .dots argument and put the interp work in a list.

a %>%
    filter_(~x == tag) %>%
    group_by_(tag) %>%
    summarise_(.dots = setNames(list(interp(~sum(var, na.rm = TRUE),
                                            var = as.name(paste0(metric,"_",run1)))), 
                                                            paste0(metric,"_",run1)))

Source: local data frame [1 x 2]

  2011 y_zm
1 2011   30

You could also add a rename_ step to do the same thing. I could see this being less ideal, as it relies on knowing the name you used in summarise_. But if you always use the same name, like variable_name, this does seem like a viable alternative for some situations.

a %>%
    filter_(~x == tag) %>%
    group_by_(tag) %>%
    summarise_(variable_name = interp(~sum(var, na.rm = T), 
                                         var = as.name(paste0(metric,"_",run1)))) %>%
    rename_(.dots = setNames("variable_name", paste0(metric,"_",run1)))

Source: local data frame [1 x 2]

  2011 y_zm
1 2011   30

这篇关于dplyr标准评估:总结_与变量名称相加的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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