我需要使用 dplyr (下面的代码)传递一个字符串作为列名。我无法让它工作。我尝试过使用 [[name]] 和 .data[[name]] 但这不起作用。我特别需要一个解决方案,允许我将列重命名为我在变量中指定的字符串name。
colnames(mtcars) -> cols
rename_cols <- function(col) {
name = paste0(col, "_new") #I want to be able to parse this into the rename function below
mtcars %>%
rename(
name = col,
)
}
lapply(cols, rename_cols)
Run Code Online (Sandbox Code Playgroud)