[R] File conca.

Val v@|kremk @end|ng |rom gm@||@com
Tue Nov 5 04:33:10 CET 2019


Hi All,

I have data files in several folders and want combine all  these files
in one file.  In each folder  there are several files  and these
files have the same structure but different names.   First, in each
folder  I want to concatenate(rbind) all files in to one file. While I
am  reading each files and concatenating (rbind) all files, I want to
added  the folder name as one variable  in each row. I am reading the
folder names  from a file and for demonstration I am using only two
folders  as shown below.
Data\week1             # folder name 1
           WT13.csv
           WT26.csv           ...
           WT10.csv
Data\week2            #folder name 2
           WT02.csv
           WT12.csv

Below please find  my attempt,

folders=c("week1","week2")
for(i in folders){
  path=paste("\data\"", i , sep = "")
  setwd(path)
  Flist = list.files(path,pattern = "^WT")
  dataA =  lapply(Flist, function(x)read.csv(x, header=T))
  Alldata = do.call("rbind", dataA)     # combine all files
  Alldata$foldername=i                  # adding the folder name
}
The above works for  for one folder but how can I do it for more than
one folders?

Thank you in advance,



More information about the R-help mailing list