gpt4 book ai didi

r - dplyr 重命名不适用于正则表达式

转载 作者:行者123 更新时间:2023-12-04 10:49:39 25 4
gpt4 key购买 nike

当我尝试根据某些条件重命名变量时,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 会提示这个。如果这种行为是有意的,那么使用starts_with(或包含)重命名变量同时保留其他变量的正确方法是什么?

最佳答案

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
...

关于r - dplyr 重命名不适用于正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30530050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com