将变量名称粘贴到mutate(dplyr)中 [英] Paste variable name in mutate (dplyr)

查看:125
本文介绍了将变量名称粘贴到mutate(dplyr)中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在mutate_()函数(dplyr)中使用paste()创建一个变量。

I try to create a variable with paste() in a mutate_() function (dplyr).

我尝试使用此答案修改代码( dplyr-mutate:使用动态变量名),但它不起作用...

I try to adapt code with this answer (dplyr - mutate: use dynamic variable names) but it doesn't work ...

注意:nameVarPeriod1是函数的参数

nameVarPeriod1=A2
df <- df %>%
    group_by(segment) %>%
    mutate_((.dots=setNames(mean(paste0("Sum",nameVarPeriod1)), paste0("MeanSum",nameVarPeriod1))))

这将返回一个警告:

Warning message:
In mean.default(paste0("Sum", nameVarPeriod1)) :
  argument is not numeric or logical: returning NA

如何将paste0中的字符串作为变量名求值?

How to evaluate the string in paste0 as variable name ?

当我用它替换paste0时,它可以正常工作:

When I replace the paste0 by this it works fine :

df <- df %>%
    group_by(segment) %>%
    mutate(mean=mean(SumA2))

数据:

structure(list(segment = structure(c(5L, 1L, 4L, 2L, 2L, 14L, 
11L, 6L, 14L, 1L), .Label = c("Seg1", "Seg2", "Seg3", "Seg4", 
"Seg5", "Seg6", "Seg7", "Seg8", "Seg9", "Seg10", "Seg11", "Seg12", 
"Seg13", "Seg14"), class = "factor"), SumA2 = c(107584.9, 127343.87, 
205809.54, 138453.4, 24603.46, 44444.39, 103672, 88695.8, 64400, 
36815.82)), .Names = c("segment", "SumA2"), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))


推荐答案

dplyr 0.7.0 起不需要使用 mutate _ 。这是一个使用:= 来动态分配变量名和辅助函数的解决方案。

dplyr 0.7.0 onwards does not require use of mutate_. Here is a solution using := to dynamically assign variable names and helper functions quo name.

阅读 vignette( programming, dplyr)会有所帮助。另请参见在dplyr的较旧版本中,在`dplyr` 中使用动态变量名称。

It will be helpful to read vignette("programming", "dplyr") for more info. See also Use dynamic variable names in `dplyr` for older versions of dplyr.

df <- df %>%
  group_by(segment) %>%
  mutate( !!paste0('MeanSum',quo_name(nameVarPeriod1)) := 
mean(!!as.name(paste0('Sum',quo_name(nameVarPeriod1)))))



dplyr 1.0.0替代:



使用新的 dplyr 1.0.0 中的> cross 函数(当前仅适用于github remotes :: install_github('tidyverse / dplyr')) 我们可以使用 glue 样式语法设置名称,并且可以在名称中包括函数名称和原始列:

dplyr 1.0.0 alternative:

Using the new across function in dplyr 1.0.0 (currently only on github remotes::install_github('tidyverse/dplyr') we can set names using glue style syntax and can include the function name and original column as part of the name:

my_fn <- function(nameVarPeriod1 = 'A2'){
  col_list <- paste0('Sum',nameVarPeriod1)
  df %>% 
    group_by(segment) %>%
    mutate(across(col_list, list(mean=mean), .names = "{fn}{col}"))
}

my_fn()
#   segment   SumA2 meanSumA2
#   <fct>     <dbl>     <dbl>
# 1 Seg5    107585.   107585.
# 2 Seg1    127344.    82080.
# 3 Seg4    205810.   205810.
# 4 Seg2    138453.    81528.
# 5 Seg2     24603.    81528.
# 6 Seg14    44444.    54422.
# 7 Seg11   103672    103672 
# 8 Seg6     88696.    88696.
# 9 Seg14    64400     54422.
#10 Seg1     36816.    82080.

这篇关于将变量名称粘贴到mutate(dplyr)中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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