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

Extracting column name from CSV filename during import

I have 24 CSV files to import, each with three columns of data: type (CHR), buffer (INT), mean (NUM). I import these using:

data <- list.files(path = "...", pattern = "*.csv", full.names = TRUE) %>% 
lapply(read_csv) %>%                                            
bind_rows

This creates a ‘long’ data table containing three columns of data.

To enable monthly analysis I need to extract a date from the filename of each imported CSV to create a ‘wide’ table (with a column for each date), or a ‘long’ table with date as another column).

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

Although I’ve found quite a few suggested ways of doing this I can’t get anything to work (I’m relatively new to R) and have become a b it confused by unfamiliar syntax.

The CSV filename is in the format ABC_YYYY-MM-DD.CSV

>Solution :

This would create a long table with date as 4th column –

library(tidyverse)

data <- list.files(path = "...", pattern = "*.csv", full.names = TRUE) %>%
          sapply(read_csv, simplify = FALSE) %>%
          imap_dfr(~.x %>% 
          mutate(date = sub('.*(\\d{4}-\\d{2}-\\d{2}).*', '\\1', basename(.y))))

sapply with simplify = FALSE would create a list with names of the list as file name. Using imap_dfr we combine all the data in one dataframe and create a new column date extract the date from the list 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