[R] File conca.

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Tue Nov 5 09:29:45 CET 2019

I recommend not using setwd unless you have to (e.g at the beginning of a script run by cron or another task scheduler). It is much simpler to build paths to directories and files using file.path.

On November 5, 2019 12:13:19 AM PST, PIKAL Petr <petr.pikal using precheza.cz> wrote:
>Help with such operations is rather tricky as only you know exact
>of your folders.
>see some hints in line
>> -----Original Message-----
>> From: R-help <r-help-bounces using r-project.org> On Behalf Of Val
>> Sent: Tuesday, November 5, 2019 4:33 AM
>> To: r-help using R-project.org (r-help using r-project.org)
><r-help using r-project.org>
>> Subject: [R] File conca.
>> Hi All,
>> I have data files in several folders and want combine all  these
>files in
>> 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
>> folder name as one variable  in each row. I am reading the folder
>> from a file and for demonstration I am using only two folders  as
>> 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)
>you should use 
>wd <- setwd(path)
>which keeps the original directory for subsequent use
>>   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
>now you can do
>to return to original directory
>> The above works for  for one folder but how can I do it for more than
>> folders?
>You also need to decide if you want all data from all folders in one
>called Alldata or if you want several Alldata objects, one for each
>In second case you could use list structure for Alldata. In the first
>you could store data from each folder in some temporary object and use
>something like
>temp <- do.call("rbind", dataA)
>temp$foldername <- i
>Alldata <- temp
>in the first cycle
>Alldata <- rbind(Alldata, temp)
>in second and all others.
>Or you could initiate first Alldata manually and use only
>Alldata <- rbind(Alldata, temp)
>in your loop.
>> Thank you in advance,
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-
>> guide.html
>> and provide commented, minimal, self-contained, reproducible code.

Sent from my phone. Please excuse my brevity.

More information about the R-help mailing list