[英]Renaming multiple similar column names
I have a data frame that has columns ranging from start_1 to start_32.我有一个数据框,其列范围从 start_1 到 start_32。 I'd like to rename all of them to m1 to m32.
我想将它们全部重命名为 m1 到 m32。 So far I have done this using the following code:
到目前为止,我已经使用以下代码完成了这项工作:
df %>%
rename(m1 = start_1,
m2 = start_2,
...
m32 = start_32)
I may need to do this a few more times for other variables and I am looking for more condensed way of achieving the same goal.对于其他变量,我可能需要多做几次,我正在寻找更简洁的方法来实现相同的目标。 Thanks!
谢谢!
You can use names
:您可以使用
names
:
names(df) <- gsub("^start_", "m", names(df))
or, in dplyr
, with rename_with
:或者,在
dplyr
中,使用rename_with
:
library(dplyr)
rename_with(df, ~ gsub("start_", "m", .x), starts_with("start_"))
rename_with(df, ~ gsub("^start_", "m", .x))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.