How to fix spaces in column names of a data.frame (remove spaces, inject dots)?

后端 未结 12 1217
我寻月下人不归
我寻月下人不归 2020-12-07 15:28

After importing a file, I always try try to remove spaces from the column names to make referral to column names easier.

Is there a better way to do this other then

相关标签:
12条回答
  • 2020-12-07 16:16

    best solution I found so far is

    names(ctm2) %<>% stringr::str_replace_all("\\s","_") %>% tolower
    

    credit goes to commenters and other answers

    0 讨论(0)
  • 2020-12-07 16:17

    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
    
    0 讨论(0)
  • 2020-12-07 16:20

    Just assign to names(ctm2):

      names(ctm2) <- c("itsy", "bitsy", "eeny", "meeny")
    

    or in data-driven way:

      names(ctm2) <- paste("myColumn", 1:ncol(ctm2), sep="")
    

    Another possibility is to edit your source file...

    0 讨论(0)
  • 2020-12-07 16:26

    You can also use combination of make names and gsub functions in R.

    names(ctm2)<- gsub("\\.","_", make.names(names(ctm2), unique = T))
    

    Above code will do 2 things at a time:

    1. It will create unique names for all columns - for e.g. same names will be converted to unique e.g. c("ab","ab") will be converted to c("ab","ab2")
    2. It will replace dots with Underscores. it becomes easy (just double click on name) when you try to select column name which has underscore as compared to column names with dots. selecting column names with dots is very difficult.
    0 讨论(0)
  • 2020-12-07 16:30

    If you use read.csv() to import your data (which replaces all spaces " " with ".") you can replace these instead with an underscore "_" using:

    names(df) <- gsub("\\.", "_", names(df))
    
    0 讨论(0)
  • 2020-12-07 16:31

    Alternatively, you may be able to achieve the same results with the stringr package.

    names(ctm2) <- names(ctm2) %>% stringr::str_replace_all("\\s","_")

    0 讨论(0)
提交回复
热议问题