如何使用整洁的评估语义 select、复制和重命名 tibble 中的多个列?

How to select, duplicate, and rename multiple columns in tibble with tidy evaluation semantics?

我想在我的 tibble 中复制一组变量,这样我就可以在下游评估中获得 variable_unmodifiedvariable 值。我想出了一个使用旧式下划线 NSE select_() 函数和 .dots 的 hacky 版本,但我想使用更新的 NSE 整洁评估语义方法。

这就是我想要的:

tibble_to_max <- tibble(
  "a_col" = c("1", "2", "3", "4"),
  "max_1" = c("3;4", "2{3}4", "7", ".{1}"),
  "max_2" = c("3;4", "2{3}4", "7", ".{1}")
)

cols_to_max <- c("max_1", "max_2")

unparsed_names <-  paste0(cols_to_max, "_unparsed")

tibble_to_max %>%
  bind_cols(select_(., .dots = setNames(cols_to_max, unparsed_names)))

输出:

# A tibble: 4 x 5
  a_col max_1 max_2 max_1_unparsed max_2_unparsed
  <chr> <chr> <chr>          <chr>          <chr>
1     1   3;4   3;4            3;4            3;4
2     2 2{3}4 2{3}4          2{3}4          2{3}4
3     3     7     7              7              7
4     4  .{1}  .{1}           .{1}           .{1}

但是如果我尝试用 select()!! 来做,.dots 不会像我预期的那样工作:

tibble_to_max %>%
  bind_cols(select(., .dots = setNames(!!cols_to_max, !!unparsed_names)))

列未按需要命名:

# A tibble: 4 x 5
  a_col max_1 max_2 .dots1 .dots2
  <chr> <chr> <chr>  <chr>  <chr>
1     1   3;4   3;4    3;4    3;4
2     2 2{3}4 2{3}4  2{3}4  2{3}4
3     3     7     7      7      7
4     4  .{1}  .{1}   .{1}   .{1}

正确的做法是什么?此外,避免将 unparsed_names 定义为单独变量的奖励积分...

也许是这样的

您的数据

tibble_to_max <- tibble(
  "a_col" = c("1", "2", "3", "4"),
  "max_1" = c("3;4", "2{3}4", "7", ".{1}"),
  "max_2" = c("3;4", "2{3}4", "7", ".{1}")
)

解决方案使用nest,然后一次复制所有嵌套数据,然后unnest。我使用 rename_all 重命名 data_copy

中的列
library(tidyverse)
tibble_to_max %>%
  nest(-a_col) %>%
  mutate(data_copy = data) %>%
  mutate(data_copy = map(data_copy, ~.x %>% rename_all(funs(paste0(., "_unparsed"))))) %>% 
  unnest(data, data_copy)

输出

# A tibble: 4 x 5
  a_col max_1 max_2 max_1_unparsed max_2_unparsed
  <chr> <chr> <chr>          <chr>          <chr>
1     1   3;4   3;4            3;4            3;4
2     2 2{3}4 2{3}4          2{3}4          2{3}4
3     3     7     7              7              7
4     4  .{1}  .{1}           .{1}           .{1}

感谢@CPak 让我走上了正确的道路。这完成了我的目标,并使用整洁的评估语义而不是 select_():

tibble_to_max <- tibble(
  "a_col" = c("1", "2", "3", "4"),
  "max_1" = c("3;4", "2{3}4", "7", ".{1}"),
  "max_2" = c("3;4", "2{3}4", "7", ".{1}")
)

cols_to_max <- c("max_1", "max_2")

tibble_to_max %>%
  bind_cols(
    select_at(., 
      .vars = !!cols_to_max, 
      .funs = funs(paste0(., "_unparsed"))
      )
    )