Skip to content
Prev 313961 / 398506 Next

how to read different files into different objects in one time?

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: