且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在dplyr链中动态重命名列名

更新时间:2023-11-18 23:31:10

您可以使用 rename_at .如果您知道只有一列以"ge"开头,那么它将起作用:

You can use rename_at. If you know that only one column starts with "ge", this will work:

library(dplyr)
mtcars %>% 
  rename_at(vars(starts_with("ge")), funs(paste0("garbage")))

如果要重命名多个列,则 funs()中的函数需要返回名称向量,或执行类似 gsub()的操作现有列名的内容.

If you want to rename more than one column, the function in funs() needs to return a vector of names, or do something like gsub() to add something to the existing column names.