在dplyr函数中创建和访问动态列名

在dplyr函数中创建和访问动态列名,r,dplyr,tidyr,rlang,tidyeval,R,Dplyr,Tidyr,Rlang,Tidyeval,但也不是更好。有没有其他方法可以实现同样的行为(我不想改变df的形状,这部分不由我决定),但可以消除rlang依赖性?这目前还可以,但如果可能的话,我想要更干净/更容易阅读的东西。如果不明显的话,我对R中的元编程还比较陌生,尽管我在其他语言中也有经验。这可能是一个更简单的版本 do.some.stuff <- function(data, foo.col){ sum.col = paste(expr_text(enexpr(foo.col)), "sum", sep

但也不是更好。有没有其他方法可以实现同样的行为(我不想改变df的形状,这部分不由我决定),但可以消除rlang依赖性?这目前还可以,但如果可能的话,我想要更干净/更容易阅读的东西。如果不明显的话,我对R中的元编程还比较陌生,尽管我在其他语言中也有经验。

这可能是一个更简单的版本

do.some.stuff <- function(data, foo.col){
  sum.col = paste(expr_text(enexpr(foo.col)), "sum", sep=".")
  max.col = paste(expr_text(enexpr(foo.col)), "max", sep=".")
  cnt.col = paste(expr_text(enexpr(foo.col)), "cnt", sep=".")
  
  select(data, date, {{ foo.col }}) %>% 
    filter(!is.na(date) & !is.na({{ foo.col }})) %>% mutate(
      cnt.col := cumsum( !is.na({{ foo.col }}) ),
      sum.col := cumsum({{ foo.col }}),
      max.col := cummax( {{ parse_expr(sum.col) }} ),
      "{{ foo.col }}.mu" :=  {{ parse_expr(sum.col) }} / {{ parse_expr(cnt.col) }}
    )
}
库(rlang)
图书馆(dplyr)
图书馆(lubridate)
示例=tible(
日期=今天()+c(1:6),
foo=rnorm(6),
)
#这是代码的初始版本。
做点什么
过滤器(!is.na(date)&!is.na({{foo.col}}))%>%mutate(
“{{foo.col}}.cnt”:=cumsum(!is.na({foo.col})),
“{{foo.col}}.sum”:=cumsum({{foo.col}}),
“{{foo.col}}.max”:=cummax({{sum.col}}),
“{{foo.col}}.mu”:={{sum.col}}/{cnt.col}
)
}
#这是我的版本,其中foo.col是一个字符参数
do.some.stuff_2%选择(日期,!!foo.col)%>%
过滤器(!is.na(日期)&!is.na(!!foo.col))%>%
变异(
#这里作为foo.col是一个添加新列的字符,只需将它们组合在一起即可
!!paste0(foo.col,“.cnt”):=cumsum(!is.na(.data[[foo.col]]),
!!paste0(foo.col,“.sum”):=cumsum(.data[[foo.col]]),
!!paste0(foo.col,“.max”):=cummax(.data[[paste0(foo.col,“.sum”)]),
!!paste0(foo.col,“.mu”):=.data[[paste0(foo.col,“.sum”)]/
.data[[paste0(foo.col,“.cnt”)]]
)
}
相同(do.some.stuff(示例,foo),do.some.stuff_2(示例,“foo”))

您可以在这里了解更多信息:

.names
参数中使用
,或者如果foo\u cnt等带有下划线是可以的,那么只需忽略
.names
参数,因为这是默认值

library(rlang)
library(dplyr)
library(lubridate)

example = tibble(
  date = today() + c(1:6),
  foo = rnorm(6), 
)

# This is your initial version of the code.
do.some.stuff <- function(data, foo.col){
  sum.col = parse_expr(paste(expr_text(enexpr(foo.col)), "sum", sep="."))
  max.col = parse_expr(paste(expr_text(enexpr(foo.col)), "max", sep="."))
  cnt.col = parse_expr(paste(expr_text(enexpr(foo.col)), "cnt", sep="."))
  
  select(data, date, {{ foo.col }}) %>% 
    filter(!is.na(date) & !is.na({{ foo.col }})) %>% mutate(
      "{{ foo.col }}.cnt" := cumsum( !is.na({{ foo.col }}) ),
      "{{ foo.col }}.sum" := cumsum({{ foo.col }}),
      "{{ foo.col }}.max" := cummax( {{ sum.col }} ),
      "{{ foo.col }}.mu" :=  {{ sum.col }} / {{ cnt.col }}
    )
}

# Here is my version where foo.col is a character param
do.some.stuff_2 <- function(data, foo.col) {
  data %>% select(date, !!foo.col) %>% 
    filter(!is.na(date) & !is.na(!!foo.col)) %>%
    mutate(
      # Here as foo.col is a character to add new column just combine them together
      !!paste0(foo.col, ".cnt") := cumsum(!is.na(.data[[foo.col]])),
      !!paste0(foo.col, ".sum") := cumsum(.data[[foo.col]]),
      !!paste0(foo.col, ".max") := cummax(.data[[paste0(foo.col, ".sum")]]),
      !!paste0(foo.col, ".mu") :=  .data[[paste0(foo.col, ".sum")]] / 
                                   .data[[paste0(foo.col, ".cnt")]]
    )
}

identical(do.some.stuff(example, foo), do.some.stuff_2(example, "foo"))
库(dplyr)
图书馆(tibble)

do.some.stuff.2直接访问
.data
是否不受欢迎?我不熟悉R的特定约定或礼仪,但在许多其他语言中,直接访问点或下划线变量会被视为违反封装。dplyr的这种行为正常吗?您可以在此处查看更多文档
.data
dplyr
的一个内部参考,这是一个漂亮的想法
foo.max
是通过在
foo.sum
上应用
cummax
来计算的。修正了。
library(rlang)
library(dplyr)
library(lubridate)

example = tibble(
  date = today() + c(1:6),
  foo = rnorm(6), 
)

# This is your initial version of the code.
do.some.stuff <- function(data, foo.col){
  sum.col = parse_expr(paste(expr_text(enexpr(foo.col)), "sum", sep="."))
  max.col = parse_expr(paste(expr_text(enexpr(foo.col)), "max", sep="."))
  cnt.col = parse_expr(paste(expr_text(enexpr(foo.col)), "cnt", sep="."))
  
  select(data, date, {{ foo.col }}) %>% 
    filter(!is.na(date) & !is.na({{ foo.col }})) %>% mutate(
      "{{ foo.col }}.cnt" := cumsum( !is.na({{ foo.col }}) ),
      "{{ foo.col }}.sum" := cumsum({{ foo.col }}),
      "{{ foo.col }}.max" := cummax( {{ sum.col }} ),
      "{{ foo.col }}.mu" :=  {{ sum.col }} / {{ cnt.col }}
    )
}

# Here is my version where foo.col is a character param
do.some.stuff_2 <- function(data, foo.col) {
  data %>% select(date, !!foo.col) %>% 
    filter(!is.na(date) & !is.na(!!foo.col)) %>%
    mutate(
      # Here as foo.col is a character to add new column just combine them together
      !!paste0(foo.col, ".cnt") := cumsum(!is.na(.data[[foo.col]])),
      !!paste0(foo.col, ".sum") := cumsum(.data[[foo.col]]),
      !!paste0(foo.col, ".max") := cummax(.data[[paste0(foo.col, ".sum")]]),
      !!paste0(foo.col, ".mu") :=  .data[[paste0(foo.col, ".sum")]] / 
                                   .data[[paste0(foo.col, ".cnt")]]
    )
}

identical(do.some.stuff(example, foo), do.some.stuff_2(example, "foo"))
library(dplyr)
library(tibble)

do.some.stuff.2 <- function(data, col) {
  cnt <- function(x) cumsum(!is.na(x))
  mx <- function(x) cummax(cumsum(x))      
  mu <- function(x) cumsum(x) / cnt(x)
  data %>%
    select(date, {{col}}) %>%
    filter(!is.na(date) & !is.na({{col}})) %>%
    mutate(across({{col}}, lst(cnt, sum=cumsum, max=mx, mu), .names = "{.col}.{.fn}" ))
}
# test
do.some.stuff.2(example, foo)
# A tibble: 6 x 6
  date             foo foo.cnt   foo.sum   foo.max    foo.mu
  <date>         <dbl>   <int>     <dbl>     <dbl>     <dbl>
1 2021-02-11 -0.000202       1 -0.000202 -0.000202 -0.000202
2 2021-02-12  0.363          2  0.363     0.363     0.181   
3 2021-02-13  1.27           3  1.63      1.63      0.543   
4 2021-02-14  1.50           4  3.13      3.13      0.781   
5 2021-02-15  1.00           5  4.13      4.13      0.826   
6 2021-02-16 -0.458          6  3.67      4.13      0.612