[R] how to read different files into different objects in one time?

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Thu Dec 27 09:08:51 CET 2012


Man, if that medicine doesn't cure you of wanting to create dozens of different objects in memory, I don't know what will. Let's see:

1) Creates a potentially large number of global objects with names unrelated to their content
2) Creates and updates a global variable for future race condition debugging fun
3) Reinvents the name lookup symbol table metadata of environments or lists, without the actual functionality
4) Uses obscure, experimental functions
5) Includes creative spelling to force the user to pay close attention in order to use the resulting metadata

and I still remain unconvinced that the result of this headache (an indefinitely large set of separate variables in the global environment) has any redeeming value. Sometimes it is better to re-phrase the question than to answer it regardless of cost.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

Heramb Gadgil <heramb.gadgil at gmail.com> wrote:

>You can try this one too.
>
>#Set the directory to a path where all the files to be read are stored
>
>TabletoRead=list.files(pattern=".txt")
>I_Step=unlist(lapply(TabletoRead,function(tab){
>                     srno<<-ifelse(exists("srno"),(1+srno),1)
>                     Temp=read.table(tab,header=T,sep="#Seperator")
>                     makeActiveBinding(paste("Table",srno,sep="_"),
>function() Temp, .GlobalEnv)
>       }))
>rm(I_Step)
>Ref_Table=cbind(Orignial=TabletoRead,Stored_As=paste("Table",1:length(TabletoRead),sep="_"))
>
>#When required you can check the Ref_Table to get the required table
>
>On Thu, Dec 20, 2012 at 10:41 AM, Jeff Newmiller
><jdnewmil at dcn.davis.ca.us>wrote:
>
>> The short answer is: don't try. You really don't want dozens of
>different
>> objects in memory that you didn't name yourself.
>>
>> What you want instead is a list of objects. You can start with a
>vector of
>> filenames and use lapply to create another list containing the data
>frames.
>> For convenience you can then set the list element names to the names
>of the
>> files.
>>
>> fnames <- list.files()
>> dta <- lapply( fnames, function(i){read.table(i, header= TRUE) })
>> names(dta) <- fnames
>>
>> You can access these data frames using the $ or "[[" operators.
>>
>> dta$G1.txt
>> dta[["G1.txt"]]
>> or
>> dta[[2]]
>>
>> Read more about it in the Introduction to R document supplied with R.
>>
>---------------------------------------------------------------------------
>> Jeff Newmiller                        The     .....       .....  Go
>Live...
>> DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live
>> Go...
>>                                       Live:   OO#.. Dead: OO#.. 
>Playing
>> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
>> /Software/Embedded Controllers)               .OO#.       .OO#. 
>rocks...1k
>>
>---------------------------------------------------------------------------
>> Sent from my phone. Please excuse my brevity.
>>
>> Yao He <yao.h.1988 at gmail.com> wrote:
>>
>> >Dear All
>> >
>> >I have a lot of files in a directory as follows:
>> >"02-03.txt"   "03-04.txt"   "04-05.txt"   "05-06.txt"   "06-07.txt"
>> >"07-08.txt"   "08-09.txt"
>> > "09-10.txt"   "G0.txt"      "G1.txt"      "raw_ped.txt"
>> >..........................
>> >
>> >I want to read them into different objects according to their
>> >filenames,such as:
>> >02-03<-read.table("02-03.txt",header=T)
>> >03-04<-read.table("03-04.txt",header=T)
>> >I don't want to type hundreds of read.table(),so how I read it in
>one
>> >time?
>> >I think the core problem is that I can't create different objects'
>> >name in the use of loop or sapply() ,but there may be a better way
>to
>> >do what I want.
>> >
>> >Thanks a lot
>> >
>> >Yao He
>> >
>> >Yao He
>>
>> ______________________________________________
>> 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.
>>




More information about the R-help mailing list