Home > OS >  Loop function for reading csv files and store them in a list
Loop function for reading csv files and store them in a list

Time:02-04

I have a folder in which are stored approximately 10 subfolders, containing 4 .csv files each! Each subfolder corresponds to a weather station, and each file in each subfolder contain temperature data for different period (e.g. station134_2000_2005.csv,station134_2006_2011.csv,station134_2012_2018.csv etc.) .

I wrote a loop for opening each folder, and rbind all data in one data frame but it is not very handy to do my work.

I need to create a loop so that those 4 files from each subfolder, rbined together to a dataframe, and then stored in a different "slot" in a list, or if it's easier,each station rbined csv data (namely each subfolder) to be exported from the loop as dataframe.

The code I wrote, which opens all files in all folders and create a big (rbined) data frame is:

directory <- list.files()   # to have the names of each subfolder
stations <- data.frame()    # to store all the rbined csv files

library(plyr)


for(i in directory){
  periexomena <- list.files(i,full.names = T, pattern = "\\.csv$")
  for(f in periexomena){
    data_files <- read.csv(f, stringsAsFactors = F, sep = ";", dec = ",")
    stations <- rbind.fill(data_files,stations)
  }

Does anyone knows how can I have a list with each subfolder's rbined 4 csv files data in different slot, or how can I modify the abovementioned code in order to export in different data frame, the data from each subfolder?

CodePudding user response:

Try:

slotted <- lapply(setNames(nm = directory), function(D) {
  alldat <- lapply(list.files(D, pattern="\\.csv$", full.names=TRUE),
                   function(fn) {
                     message(fn)
                     read.csv2(fn, stringsAsFactors=FALSE)
                   })
  # stringsAsFactors=F should be the default as of R-3.6, I believe
  do.call(rbind.fill, alldat)
})
  •  Tags:  
  • Related