On Fri, Oct 29, 2010 at 5:16 AM, Sarah Moens <sara...@telenet.be> wrote: > Hi all, > > I've been trying to find a solution for the problem of reading > multiple files and storing them in a variable that contains the names > by which I want to call the datasets later on. > > For example (5 filenames): > > - The filenames are stored in one variable: > filenames = paste(paste('name', '_', 1:5, sep = ''), '.csv', sep = '') > > - Subsequently I have a variable just containing the meaningful names > for the dataset > meaningfulnames = c('name1','name2'...,'name5') > > - I want to link each of these names to the data that is read > > for (i in 1:5) > { > meaningfulnames[i] = read.csv(filenames[i], header = TRUE, sep = ',') > } > > > I need to read in quite a lot of datafiles. I have a code doing this > one at a time, but since the number of datafiles I need to read will > increase in the future, I want to make sure I have a more flexible > solution for this. >
Try this: filenames <- sprintf("%s_%d.csv", "name", 1:5) L <- sapply(filenames, read.csv, simplify = FALSE) L will be a list with the data frames as components and the file names as the component names. If you wish to change the names from the filenames to some other names you can do this: names(L) <- vector.of.other.names If the data frames all have the same number of columns and the same names in the same order then you could also put them into a single data frame like this: do.call("rbind", L) or if you want to retain the knowledge of which each came from: library(lattice) do.call("make.groups", L) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com ______________________________________________ R-help@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.