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

read csv file from list but add the filename as identifier

I have list of csv files. I could read them all using read_csv.

But I would like to add the filename as identifier. How do I do it ?

library(tidyverse)

# read file names
csv_filenames <- list.files(path = "OMITTED FOR THIS EXAMPLE", 
                            full.names = TRUE)


###
csv_filenames are "One.csv", "Two.csv", "Three.csv", ....
###

# read csv files
df <- read_csv(csv_filenames)

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 :

read_csv has an argument id = ; if you specify "path", you get a column named "path" with the file names:

csv_data <- read_csv(csv_filenames, id = "path")

If you wanted just the base file name, you could add a dplyr::mutate step:

library(dplyr)
csv_data <- read_csv(csv_filenames, id = "path") %>%
  mutate(path = basename(path))
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