嘿,我正在尝试通过使用新版本的 dplyr 添加“Last_”来重命名某些列,但我一直收到此错误
Error: `across()` must only be used inside dplyr verbs.
这是我的代码data %>% rename(across(everything(), ~paste0("Last_", .)))
dplyr 版本:v1.0.2
最佳答案
我们可以使用 rename_with
而不是 rename
library(dplyr)
library(stringr)
data %>%
rename_with(~str_c("Last_", .), everything())
可重现的例子data(iris)
head(iris) %>%
rename_with(~str_c("Last_", .), .cols = everything())
# Last_Sepal.Length Last_Sepal.Width Last_Petal.Length Last_Petal.Width Last_Species
#1 5.1 3.5 1.4 0.2 setosa
#2 4.9 3.0 1.4 0.2 setosa
#3 4.7 3.2 1.3 0.2 setosa
#4 4.6 3.1 1.5 0.2 setosa
#5 5.0 3.6 1.4 0.2 setosa
#6 5.4 3.9 1.7 0.4 setosa
根据
?rename
rename() changes the names of individual variables using new_name = old_name syntax; rename_with() renames columns using a function.
并在
?across
across() makes it easy to apply the same transformation to multiple columns, allowing you to use select() semantics inside in summarise() and mutate().
描述说它在
mutate/summarise
内使用(和 transmute
?),并且没有任何其他功能的使用迹象,即它会因 select
而失败
关于使用 dplyr rename(across(,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64188671/