2

我想在我的 tibble 中复制一组变量,以便我可以在下游评估中拥有variable_unmodifiedvariable值。我想出了一个使用旧式下划线 NSEselect_()函数和的 hacky 版本.dots,但想使用更新的 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()and来执行此操作!!.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此外,避免定义为单独变量的奖励积分......

4

2 回答 2

2

也许是这样的

您的数据

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}
于 2017-10-24T17:25:45.367 回答
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"))
      )
    )
于 2017-10-24T20:35:14.527 回答