0

I have several csv files (let say A.csv, B.csv, ...) in several folders let say (F1, F2,...). I want to read all files in a way that cbind A.csv, B.csv of each folder and create main dataframe for each folder. It means I need to have n dataframe for my n folders with a unique name based on folder name.

I have tried this code to get the list of csv files.

files <- dir("/Users/.../.../...", recursive=TRUE, full.names=TRUE, pattern="\\.csv$") 

then created a function:

readFun <- function(x) { df <- read.csv(x)}

then sapply:

sapply(files, readFun)

it returns this error:

Error in read.table(file = file, header = header, sep = sep, quote = quote,  : no lines available in input

I played around with the code alot, but did not figure out how to debug it. Any help is highly appreciated.

Also, any hint on how to create main dataframe for each folder?

Thanks

10datmad
  • 11
  • 2
  • 5

1 Answers1

0

You could first get all the directory names, then create a function to read all the files from one directory combining them and writing the combined file in the same directory.

all_directories <- dir('top/directory', full.names = TRUE)

bind_files_into_one <- function(file_path) {
   all_files <- list.files(file_path, full.names = TRUE, pattern = "\\.csv$")
   temp <- do.call(cbind, lapply(all_files, read.csv))
   write.csv(temp, paste0(file_path, '/combined.csv'), row.names = FALSE)
}

We can use lapply to apply this function to every directory.

lapply(all_directories, bind_files_into_one)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Thank you, but still the same error: Error in read.table(file = file, header = header, sep = sep, quote = quote, : no lines available in input – 10datmad Apr 04 '20 at 04:40
  • @10datmad Can you check your files if they are corrupted? because I checked this code for two folder/directories with two csvs on my system and it worked fine for me. – Ronak Shah Apr 04 '20 at 05:37
  • Thank you! the issue comes from different rows in the files. – 10datmad Apr 06 '20 at 20:52