0

I need to separate csv files into their own dataframes using R.
Currently, I'm using:

file_names <- list.files(path = my_path,recursive = TRUE) 
for(i in file_names){file <- read.csv(i)}

However, I want each file in it's own dataframe so I can total each one before binding them. I was trying to use lapply:

file_names <- list.files(path = my_path, recursive = TRUE) 
file <- lapply(file_names,read.csv)
names(file) <- c("a","b","c","d","e","f","g")

but the number of files will sometimes be different. Sometimes there could be less files. How can I read the files into separate dataframes before combining them?

Dave2e
  • 22,192
  • 18
  • 42
  • 50
Eric Bunker
  • 33
  • 1
  • 5
  • 2
    I'm not sure I understand your question, it looks like you have read the separate files into separate dataframes. Do you want to then extract the dataframes back out of the list into the environment? – Esther Jun 08 '18 at 18:51
  • something like this: https://stackoverflow.com/questions/47144892/issues-with-converting-data-into-a-matrix-after-running-lapply/47145407#47145407? – acylam Jun 08 '18 at 19:13

1 Answers1

0

Use this to name your files:

names(file) <- c(letters[1:length(file)])
Vishesh Shrivastav
  • 2,079
  • 2
  • 16
  • 34