[R] Batch importing data

David Winsemius dwinsemius at comcast.net
Fri Apr 27 19:50:33 CEST 2012


On Apr 27, 2012, at 1:30 PM, R. Michael Weylandt wrote:

> list.files() will give you all the file names in your working
> directory (you can also give it a pattern argument) then can loop over
> those with something like:
>
> lapply(list.files(), read.table)
>
> which will put all your files in a list object. This is generally
> considered much more convenient than trying to create a whole bunch of
> objects with different names programmatically.

And if you wanted to have them named, then something like:

inputfils <- lapply(list.files(patt=".txt"), read.table)
names(inputfils)  <- sub("\\.txt", "", list.files(patt=.txt) )

Then this will let you access a particular file by name:

inputfils[["Oil_20030801"]]

--  
David.

>
> Michael
>
> On Fri, Apr 27, 2012 at 11:23 AM, jiangxijixzy  
> <jiangxijixzy at 163.com> wrote:
>> I want to import data from about 2000 text files, and hope to  
>> create a data
>> frame to make it easy to quote the data.
>> For example, the files like this
>> Oil_20030801.txt, Oil_20030804.txt, Oil_20030805.txt …  
>> Oil_20120427.txt
>> The dates aren’t continuous. I want to create the data frame called  
>> “Oil”,
>> like that
>> Oil20030801<-read.table(“E:/Oil/ Oil_20030801.txt”)
>> Oil20030804<-read.table(“E:/Oil/ Oil_20030804.txt”)
>> Oil20030805<-read.table(“E:/Oil/ Oil_20030805.txt”)
>>>> Oil20120427<-read.table(“E:/Oil/ Oil_20120427.txt”)
>> It is a time consuming way. How can I perform a convenient way?  
>> Thank you!
>>
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/Batch-importing-data-tp4592997p4592997.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list