I am wondering if it is possible to read.table repeatedly from a list of file names into a new list of table names. For example: filenames <- list.files() then with a function like rf <- function(i) { word??(filename[i]) <- read.table(filenames[i]) } I can't seem to find a function like word?? that will be the object of another operation. If this worked, then I could repeat it for the length of filenames. Also, even the following function seems to give me an error, but I don't yet know why. rf <- function(nam, i) { nam <- read.table(filenames[i]) } Any help would be very much appreciated. Thanks, Thomas [[alternative text/enriched version deleted]]
The solution is in section 7.21 of the R FAQ. BTW, `rf' is a built-in R function for generating random numbers from an F distribution, so better use some other name. Andy> From: thomas hills > > I am wondering if it is possible to read.table repeatedly from a list > of file names into a new list of table names. > > For example: > > filenames <- list.files() > > then with a function like > > rf <- function(i) { > word??(filename[i]) <- read.table(filenames[i]) } > > I can't seem to find a function like word?? that will be the > object of > another operation. If this worked, then I could repeat it for the > length of filenames. > > Also, even the following function seems to give me an error, but I > don't yet know why. > > rf <- function(nam, i) { nam <- read.table(filenames[i]) } > > > Any help would be very much appreciated. > > Thanks, > Thomas > [[alternative text/enriched version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
On Tue, 28 Dec 2004, thomas hills wrote:> I am wondering if it is possible to read.table repeatedly from a list > of file names into a new list of table names. > > For example: > > filenames <- list.files() > > then with a function like > > rf <- function(i) { > word??(filename[i]) <- read.table(filenames[i]) }lapply(filenames, read.table) will do what I think you want to do. [It is possible to assign each file to a variable whose name is given by another variable, and FAQ 7.21 tells you how, but that probably isn't a good idea.] -thomas
thomas hills <thills at mail.utexas.edu> writes:> I am wondering if it is possible to read.table repeatedly from a list > of file names into a new list of table names. > > For example: > > filenames <- list.files() > > then with a function like > > rf <- function(i) { > word??(filename[i]) <- read.table(filenames[i]) } > > I can't seem to find a function like word?? that will be the object of > another operation. If this worked, then I could repeat it for the > length of filenames. > > Also, even the following function seems to give me an error, but I > don't yet know why. > > rf <- function(nam, i) { nam <- read.table(filenames[i]) } > > > Any help would be very much appreciated.Something like listoftables <- lapply(filenames, read.table) names(listoftables) <- filenames might be what you are looking for. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907