0

The main idea is that I have two folders/paths now in my local machine. In each folder, I have multiple csv files files I want to read into my R. However, instead of appending them all together into one files I want all folder1 files being in file1 and all folder2 files being in file2. I only know how to append them all together, but not know how to append them into two separate files. Below are my code so far.

dirs<-list("path/folder1","path/folder2")
data<-list()
for(dir in dirs){
  ##read in the list of files in each folder
  flist<-list.files(path=dir,pattern = "\\.csv$")
  ## a second for loop to read thru what's inside each folder
  for (file in flist){message("working on",file) 
    indata<-fread(paste0(dir,file))
    data<-rbind(data,indata)}
}

So far, I think the data keeps everything into one file. so How do I do to make it save them into two different files?

xiahfyj
  • 101
  • 1
  • 5
  • you could do: `sapply(dirs,function(p)write.csv(do.call(rbind,lapply(list.files(p,full.names = TRUE,pattern = 'csv$'), read.csv)),paste0(p,"/newfile.csv")))` – Onyambu Jan 08 '20 at 23:10

2 Answers2

0

The quickest option I can think of is to try using data[[dir]] to make each directory's data its own object in the data list. Then you can access them with data$`path1` etc.

dirs<-list("path/folder1","path/folder2")
data<-list()
for(dir in dirs){
  ##read in the list of files in each folder
  flist<-list.files(path=dir,pattern = "\\.csv$")
  ## a second for loop to read thru what's inside each folder
  for (file in flist){message("working on",file) 
    indata<-fread(paste0(dir,file))
    data[[dir]]<-rbind(data[[dir]],indata)}
}

(However, it might be much nicer (and faster) to use lapply instead of for loops)

Nick
  • 496
  • 4
  • 7
0

You could assign your read in files into new R objects named by your folder number. I changed list() to c() for dirs for easier assignment with assign(). And moved the data <- list() into the first loop so it gets overwritten after each folder is completed.

dirs<-c("path/folder1","path/folder2")

for(dir in 1:length(dirs)){
  ##read in the list of files in each folder
  flist<-list.files(path=dirs[dir], pattern = "\\.csv$")

  data <- list()
  ## a second for loop to read thru what's inside each folder
  for (file in flist){message("working on", file) 
    indata<-read.csv(paste0(dirs[dir],"/",file))
    data<-rbind(data,indata)
    assign(paste0("data_",dir), data)
    }
}
neko
  • 48
  • 6