Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Clean Column Names in R

I have the following code in R that combines multiple (177) csv files. However in a lot of the files, some column names have spaces and the others have underscores as separators e.g ‘Article Number’ and ‘Article_Number’. I have tried janitor::make_clean_names and make.names etc within the code but I just cannot figure out the correct way to do it.

Any help much appreciated

df <- list_of_files %>%
  set_names() %>% 
  map_dfr(
    ~read_csv(.x, col_types = cols(.default = "c", 'TY Stock Value' = "c"), col_names = TRUE,),
    .id = "file_name"  
  )

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can add it insight the map_dfr function such that each columns get first harmoized before it gets bind together.

df <- list_of_files %>%
  set_names() %>%
  map_dfr(~ .x %>%
    read_csv(.,
      col_types = cols(.default = "c", "TY Stock Value" = "c"),
      col_names = TRUE
    )
    %>%
    janitor::clean_names(),
  .id = "file_name"
  )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading