Hi R users, I'd like to ask that how to merge several datasets into one in R? I put these csv files in one folder, and use the lapply function, but it says that cannot open file 'xx.csv'. These files have different names, but end with .csv extension, and the files have the same header. Thanks for your help. [[alternative HTML version deleted]]
Lily: If you mean that you have several csv files in a directory/folder on your computer and you are using lapply() to do something with them, then you do not have a clue about how R works and you need to go through some tutorials to learn. There are many good ones on the web. Some recommendations can be found here: https://www.rstudio.com/online-learning/#R We do expect posters here to have made some minimal effort to learn R basics before posting. If I have misunderstood, then you will either have to wait for a reply from someone with greater insight or explain yourself more clearly. Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Thu, Jun 30, 2016 at 12:26 PM, lily li <chocold12 at gmail.com> wrote:> Hi R users, > > I'd like to ask that how to merge several datasets into one in R? I put > these csv files in one folder, and use the lapply function, but it says > that cannot open file 'xx.csv'. These files have different names, but end > with .csv extension, and the files have the same header. Thanks for your > help. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Hello, Maybe something like this. fls <- list.files(pattern = "*.csv") dat.list <- lapply(fls, read.csv) dat <- do.call(rbind, dat.list) Hope this helps, Rui Barradas ? Citando lily li <chocold12 at gmail.com>:> Hi R users, > > I'd like to ask that how to merge several datasets into one in R? I put > these csv files in one folder, and use the lapply function, but it says > that cannot open file 'xx.csv'. These files have different names, but end > with .csv extension, and the files have the same header. Thanks for your > help. > > ? ? ? ? [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.htmland provide commented, > minimal, self-contained, reproducible code.? [[alternative HTML version deleted]]
Hi Lily, I think below codes can work: f<- list.files("D:/output/test/your folde rname",full.names=TRUE,recursive=TRUE) files<- grep(".csv", f) files_merge<- data.frame() for (i in 1:length(f[files])){ data<- read.csv(file=f[files][i],header=TRUE, sep=",") files_merge<- rbind(files_merge,data) } Best, Lida On Thu, Jun 30, 2016 at 2:26 PM, lily li <chocold12 at gmail.com> wrote:> Hi R users, > > I'd like to ask that how to merge several datasets into one in R? I put > these csv files in one folder, and use the lapply function, but it says > that cannot open file 'xx.csv'. These files have different names, but end > with .csv extension, and the files have the same header. Thanks for your > help. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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]]