我想在我的 tibble 中复制一组变量,以便我可以在下游评估中拥有variable_unmodified
和variable
值。我想出了一个使用旧式下划线 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
此外,避免定义为单独变量的奖励积分......