How do I add a prefix to several variable names using dplyr?

后端 未结 5 1713
花落未央
花落未央 2020-12-10 03:06

I\'m trying to add a common prefix to each of the variable names in a data.frame. For example, using the mtcars data, I could add the prefix \"cars.\" using th

5条回答
  •  半阙折子戏
    2020-12-10 03:35

    Another dplyr solution:

    I find it easiest with the dplyr rename_all, rename_at, rename_if

    Try this for renaming all column names:

    mtcars %>% rename_all(function(x){paste0("cars.", x)})
    

    Try this for renaming "some" column names:

    mtcars %>% rename_at(vars(hp:wt) ,function(x){paste0("cars.", x)})
    

提交回复
热议问题