dplyr标准评估:总结_与变量名称相加的变量 [英] dplyr standard evaluation: summarise_ with variable name for summed variable
问题描述
库(dplyr)
/ pre>
库(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))))
我试过了很多不同的事情涉及到as.name()或interp(),但似乎没有任何效果。
解决方案NSE vignette一段时间,戳戳事物,我发现如果您使用
,则可以使用
中的
参数,并将setNames
$ c> .dotsinterp
工作放在列表中。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
withinsummarise_
if you use the.dots
argument and put theinterp
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 insummarise_
. But if you always use the same name, likevariable_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屋!