使用复合在自定义构建函数中使用 dplyr (!!paste0, {{}}, as.name(), eval(parse(text=) [英] using a composite to call a variable dynmically in custom build function with dplyr (!!paste0, {{}}, as.name(), eval(parse(text=)

查看:35
本文介绍了使用复合在自定义构建函数中使用 dplyr (!!paste0, {{}}, as.name(), eval(parse(text=)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是以下问题的扩展:(1)(2) 并在评论中询问 (2)Mario Reutter.

This is an extension of the following questions: (1), (2) and also asked in the comments to (2) by Mario Reutter.

library(dplyr, tidyverse)
string <- c("car", "train", 'bike', 'plain')
speed1 <- runif(4, min = 0, max = 10000)
speed2 <- runif(4, min = 0, max = 10000)
n1  <- sample(1:100, 4)
n1_plus  <- sample(1:100, 4)
n1_minus <- sample(1:100, 4)
n2  <- sample(1:100, 4)
df <- data.frame(string, speed1, speed2, n1, n2, n1_plus, n1_minus)

感谢 akrunanswer 我可以构建以下功能:

Thanks to akrun's answer I can build the following function:

my_fun <- function(dataf, V1, V2){
dataf %>%
dplyr::mutate("{{V1}}_{{V2}}" := paste0(format({{V1}}, big.mark   = ",") ,
  '\n(' , format({{V2}}, big.mark   = ",") , ')'))}

df<-df%>%my_fun(speed1, n1)

{{V1}}_{{V2}}"定义的复合名称创建一个新变量;:=.

但是,如何在等式右侧调用复合变量名称?例如.将 format({{V2}}, big.mark = ",") 替换为类似 format('{{V2}}_plus', big.mark = ",").我试过了(不工作):

However, how were I to call a composite variable name on the right hand side of the equation? E.g. substituting format({{V2}}, big.mark = ",") with something like format('{{V2}}_plus', big.mark = ","). I tried (not working):

my_fun <- function(dataf, V1, V2){
dataf %>%
dplyr::mutate("{{V1}}_{{V2}}_plus" := paste0(format({{V1}}, big.mark   = ",") ,
  '\n(' , format('{{V2}}_plus', big.mark   = ",") , ')'))}

df<-df%>%my_fun(speed1, n1)

所需的输出:我希望有一个新列 speed1_n1_plus 组合来自 speed1n1_plus 的值:

Desired output: I would expect a new column speed1_n1_plus that combines the values from speed1and n1_plus:

  string   speed1   speed2 n1 n2 n1_plus n1_minus       speed1_n1_plus
1    car 3958.415 1049.172 70 91      25       53 3,958.415\n(25)
2  train 6203.919 8639.160 52 92      14       91 6,203.919\n(14)
3   bike 2966.391 2997.303 35 55      46       61 2,966.391\n(46)
4  plain 2755.266 1627.379 98 66       8       49 2,755.266\n( 8)

我只需要对具有相似名称的多个变量进行操作.变量名称是核心"名称(在本例中为n1",{{V2}})以及后缀和前缀的组合.我想避免为每个变量名称添加额外的参数,因为它只为核心名称添加了一个后缀.

I simply have to do operations on multiple variables with similar names. The variable names are composites of the 'core' name (in this case 'n1', {{V2}}) and suffixes and prefixes. I would like to avoid additional arguments for each variable name as it adds just a suffix to the core name.

我在尝试:!!paste0, as.name(), eval(parse(text=), ...,这可能在函数外工作,但对我来说不在函数内.

I was trying: !!paste0, as.name(), eval(parse(text=), ..., which may work outside a function, but for me not within.

推荐答案

my_fun <- function(dataf, V1, V2){
           dataf %>%
              dplyr::mutate("{{V1}}_{{V2}}_plus" := paste0(format({{V1}}, big.mark   = ","),
                  "\n(", format(!! rlang::sym(paste0(rlang::as_string(ensym(V2)), "_plus")), big.mark  = ","), ")"))}

-测试

df %>%
  my_fun(speed1, n1)
 string   speed1    speed2 n1 n2 n1_plus n1_minus  speed1_n1_plus
1    car 4453.441 3336.7287 92 97      28       56 4,453.441\n(28)
2  train 7718.381  638.5120 82 61       9       13 7,718.381\n( 9)
3   bike 4648.093 4267.8390  7 92      83       29 4,648.093\n(83)
4  plain 3815.145  793.6886 18 56      30       46 3,815.145\n(30)

这篇关于使用复合在自定义构建函数中使用 dplyr (!!paste0, {{}}, as.name(), eval(parse(text=)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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