【发布时间】:2017-10-24 17:17:56
【问题描述】:
我想在我的 tibble 中复制一组变量,以便在下游评估中获得 variable_unmodified 和 variable 值。我想出了一个使用旧式下划线 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 定义为单独变量的奖励积分...
【问题讨论】: