1
votes

I have multiple csv files in a folder none of which have a header. I want to preserve the order set out by the number at the end of the file. The file names are "output-1.csv", "output-2.csv" and so on. Is there a way to include the file name of each csv so I know which data corresponds to which file. The answer [here][1] gets close to what I want.

library(tidyverse)

#' Load the data ----
mydata <-
  list.files(path = "C:\\Users\\Documents\\Manuscripts\\experiment1\\output",  
   pattern = "*.csv") %>%
  map_df( ~ read_csv(., col_names = F))
mydata
1

1 Answers

3
votes

You can use:

library(tidyverse)

mydata <- list.files("C:\\Users\\Documents\\Manuscripts\\experiment1\\output", 
pattern = ".csv$", full.names = T) %>% 
  set_names(str_sub(basename(.), 1, -5)) %>% 
  map_dfr(read_csv, .id = "file")