Sarah Moens
2010-Oct-29 09:16 UTC
[R] Reading multiple .csv-files and assigning them to variable names
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. Thanks a lot for your help. I have tried to look in the help pages and also came across dbfread, but I can't seem to find something I can use or understand at this point. Sarah
jim holtman
2010-Oct-29 14:41 UTC
[R] Reading multiple .csv-files and assigning them to variable names
Read them into a list; much easier to handle: myList <- lapply(filenames, read.csv) On Fri, Oct 29, 2010 at 5:16 AM, Sarah Moens <sarah_m at 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. > > Thanks a lot for your help. I have tried to look in the help pages and > also came across dbfread, but I can't seem to find something I can use > or understand at this point. > > > Sarah > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Gabor Grothendieck
2010-Oct-29 15:18 UTC
[R] Reading multiple .csv-files and assigning them to variable names
On Fri, Oct 29, 2010 at 5:16 AM, Sarah Moens <sarah_m at 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