Hello, all: I have twenty datasets named as: data1.csv, data2.csv, ?, data20.csv. I am trying to read all of them into R by using loop and function read.table(), but I don't know how to handle the name of datasets. Has anybody have encountered a similar problem? Or do you have any suggestions? Your help would be greatly appreciated. Legen -- View this message in context: http://www.nabble.com/Read-in-multiple-datasets-tp25630688p25630688.html Sent from the R help mailing list archive at Nabble.com.
input <- lapply(1:20, function(.file) read.csv(paste('data', .file, '.csv', sep=''))) This will create a list of 20 with the dataframe from each file in the list. On Sat, Sep 26, 2009 at 11:47 PM, legen <legendy at gmail.com> wrote:> > Hello, all: > > I have twenty datasets named as: data1.csv, data2.csv, ?, data20.csv. I am > trying to read all of them into R by using loop and function read.table(), > but I don't know how to handle the name of datasets. Has anybody have > encountered a similar problem? Or do you have any suggestions? Your help > would be greatly appreciated. > > Legen > > -- > View this message in context: http://www.nabble.com/Read-in-multiple-datasets-tp25630688p25630688.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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?
Try this: filenames <- sprintf("data%d.csv", 1:20) DFs <- sapply(filenames, read.csv, simplify = FALSE) which will return a list of data frames, DFs, each named by its filename so that DFs[[1]] or DFs[["data1.csv"]] give the data frame read from data1.csv, etc. and names(DFs) gives a vector of their names as does filenames. On Sat, Sep 26, 2009 at 11:47 PM, legen <legendy at gmail.com> wrote:> > Hello, all: > > I have twenty datasets named as: data1.csv, data2.csv, ?, data20.csv. I am > trying to read all of them into R by using loop and function read.table(), > but I don't know how to handle the name of datasets. Has anybody have > encountered a similar problem? Or do you have any suggestions? Your help > would be greatly appreciated. > > Legen > > -- > View this message in context: http://www.nabble.com/Read-in-multiple-datasets-tp25630688p25630688.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >