我想在变量中指定的列上使用 gsub 参数来改变数据框的列,但我正在努力应对非标准评估。
在这个玩具示例中,我想在调用 gsub时使用columns[[1]]
andcolumns[[2]]
而不是.$name_A
and 。.$name_B
我可以,还是需要重新考虑我的方法?
library(tidyverse)
test_df <- tibble(name_A =
c("asdf", "ghjk"),
name_B =
c("qwer", "tyui"))
columns <- c("name_A", "name_B")
test_df %>%
mutate(new_col_A =
gsub(pattern = 'asdf', replacement = 'NEW_VALUE_A', x = .$name_A),
new_col_B =
gsub(pattern = 'tyui', replacement = 'NEW_VALUE_B', x = .$name_B))