dplyr重命名不使用正则表达式

dplyr重命名不使用正则表达式,r,dplyr,R,Dplyr,当我尝试根据特定条件重命名变量时,select函数工作正常 require(dplyr) select(iris, petal = starts_with("Petal")) 但是,当我尝试使用 rename(iris, petal = starts_with("Petal")) Error: Arguments to rename must be unquoted variable names. Arguments petal are not. 我不知道dplyr为什么会对此抱怨。如果要

当我尝试根据特定条件重命名变量时,select函数工作正常

require(dplyr)
select(iris, petal = starts_with("Petal"))
但是,当我尝试使用

rename(iris, petal = starts_with("Petal"))

Error: Arguments to rename must be unquoted variable names. Arguments petal are not.

我不知道dplyr为什么会对此抱怨。如果要使用此行为,那么在保留其他变量的同时使用start_with(或contains)重命名变量的正确方法是什么?

select
已经为您重命名了这些变量。您可以向调用中添加
everything()
,以获取其余列

select(iris, petal = starts_with("Petal"), everything())
#     petal1 petal2 Sepal.Length Sepal.Width    Species
# 1      1.4    0.2          5.1         3.5     setosa
# 2      1.4    0.2          4.9         3.0     setosa
# 3      1.3    0.2          4.7         3.2     setosa
# 4      1.5    0.2          4.6         3.1     setosa
# 5      1.4    0.2          5.0         3.6     setosa
# 6      1.7    0.4          5.4         3.9     setosa
# 7      1.4    0.3          4.6         3.4     setosa
# 8      1.5    0.2          5.0         3.4     setosa
# 9      1.4    0.2          4.4         2.9     setosa
...