Replacement for “rename” in dplyr

前端 未结 6 908
长情又很酷
长情又很酷 2020-12-02 05:51

I like plyr\'s renaming function rename. I have recently started using dplyr, and was wondering if there is an easy way to rename variables using a function fr

6条回答
  •  一生所求
    2020-12-02 06:11

    While not exactly renaming, dplyr::select_all() can be used to reformat column names. This example replaces spaces and periods with an underscore and converts everything to lower case:

    iris %>%  
      select_all(~gsub("\\s+|\\.", "_", .)) %>% 
      select_all(tolower) %>% 
      head(2)
      sepal_length sepal_width petal_length petal_width species
    1          5.1         3.5          1.4         0.2  setosa
    2          4.9         3.0          1.4         0.2  setosa
    

提交回复
热议问题