Hi: An alternative approach with a few less keystrokes, using the plyr package:
library(plyr) # Create some example data files, populate them and write them out using write.csv: fnames <- c(paste('file0', 1:9, '.csv', sep = '')) for(i in seq_along(fnames)) { d <- data.frame(x = rnorm(3), y = rpois(3, 10), z = round(runif(3), 3)) write.csv(d, fnames[i], row.names = FALSE, quote = FALSE) } This generates files file01.csv -> file09.csv. Each file has three lines with three variables. Now use ldply to read them back in using read.csv(): u <- ldply(fnames, read.csv) > u x y z 1 -0.77367688 17 0.496 2 -0.79069791 11 0.323 3 0.69257133 9 0.229 4 0.55484202 14 0.428 5 -0.67254503 4 0.702 6 0.15010483 10 0.802 ... [27 lines in all] ldply() is one way to circumvent the do.call(rbind, lapply(...)) mantra when the final output is to be a data frame. HTH, Dennis On Fri, Oct 8, 2010 at 8:30 AM, Joshua Wiley <jwiley.ps...@gmail.com> wrote: > Hi Xinli, > > You will probable have to tweak this some for it to work for you, but > it at least gives you an idea. > > First put all your files in one directory, then you use the > list.files() function to read into R the names of every file in that > directory (this is easier than typing all 100 something names). Now, > you can use lapply() to 'apply' the function, read.csv() to each file > name. In my example, I set header = TRUE just to show you how you can > specify arguments to the function you are calling with lapply(). This > will result in a list with each element being the results of > read.csv(). Finally, since all the columns are the same, we can just > rbind() every data frame together. That is accomplished the outer > function, do.call(), whose first argument is "rbind" and second is the > output of lapply(). > > filenames <- list.files(path = "~/") > do.call("rbind", lapply(filenames, read.csv, header = TRUE)) > > Hope that helps, > > Josh > > On Fri, Oct 8, 2010 at 4:32 AM, XINLI LI <lihaw...@gmail.com> wrote: > > Hi Joshua: > > > > Thank you very much for your help. I have more than 100 files, and do > > you have a better way to merge the files into one dataset, there all have > > the same columns. > > > > Thanks, > > > > xinli > > > > On Thu, Oct 7, 2010 at 11:28 PM, Joshua Wiley <jwiley.ps...@gmail.com> > > wrote: > >> > >> Hi Xing, > >> > >> This depends somewhat on what you mean by "merge", and how many files > >> you are talking about. Supposing you are dealing with few enough > >> files you can do it manually: > >> > >> dat1 <- read.csv("yourfile1.csv") > >> dat2 <- read.csv("yourfile2.csv") > >> ... > >> datn <- read.csv("yourfilen.csv") > >> > >> If each file contains unique variables: > >> > >> complete.dat <- cbind(dat1, dat2, ... , datn) > >> > >> if each one is just a continuation rowise: > >> > >> complete.dat <- rbind(dat1, dat2, ... , datn) > >> > >> If they are all sort of related but in no consistent way, and do not > need > >> to be: > >> > >> complete.dat <- list(dat1, dat2, ... , datn) > >> > >> If you need some fancier merging than just columnwise or rowwise, look > >> at merge(). For documentation on these features see: > >> > >> ?data.frame # to find out more about data frames (which is what read.csv > >> uses) > >> ?list # for details about what a list is > >> ?read.table > >> ?read.csv # just a special wrapper for read.table > >> ?cbind # for column binding > >> ?rbind # for row binding > >> ?merge # for more specialized merging > >> example(merge) # for examples using merge() > >> > >> HTH, > >> > >> Josh > >> > >> On Thu, Oct 7, 2010 at 8:19 PM, XINLI LI <lihaw...@gmail.com> wrote: > >> > Dear R Group: > >> > > >> > How to import multiple csv files and merge into one dataset. > >> > > >> > Thanks and Regards, > >> > > >> > Xing > >> > > >> > [[alternative HTML version deleted]] > >> > > >> > ______________________________________________ > >> > 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. > >> > > >> > >> > >> > >> -- > >> Joshua Wiley > >> Ph.D. Student, Health Psychology > >> University of California, Los Angeles > >> http://www.joshuawiley.com/ > > > > > > > > -- > Joshua Wiley > Ph.D. Student, Health Psychology > University of California, Los Angeles > http://www.joshuawiley.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. > [[alternative HTML version deleted]] ______________________________________________ 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.