Hans W. Borchers
2008-Apr-01 15:44 UTC
[R] Applying rbind() to a sequence of data frame names
I have a set of data frames ds1, ds2, ... each having the same columns and column names: ds1 <- data.frame(x=c(1,2,3,4), y=c(5,6,7,8)) ds1 <- data.frame(x=c(9,10,11,12), y=c(13,14,15,16)) ... and I would like to combine them into just one data frame like ds <- rbind(ds1, ds2, ...) Because there are so many of them, I will have to use a character array nms <- c("ds1", "ds2", ...) How can I use this character array to generate a command using rbind() on all of these data frames at once. I tried to apply eval() somehow, but did not come up with the right idea. Thanks
Henrique Dallazuanna
2008-Apr-01 16:15 UTC
[R] Applying rbind() to a sequence of data frame names
Try this: do.call(rbind, lapply(ls(patt="^ds[0-9]"), get)) On 01/04/2008, Hans W. Borchers <hwborchers at gmail.com> wrote:> I have a set of data frames ds1, ds2, ... each having the same columns > and column names: > > ds1 <- data.frame(x=c(1,2,3,4), y=c(5,6,7,8)) > ds1 <- data.frame(x=c(9,10,11,12), y=c(13,14,15,16)) > ... > > and I would like to combine them into just one data frame like > > ds <- rbind(ds1, ds2, ...) > > Because there are so many of them, I will have to use a character array > > nms <- c("ds1", "ds2", ...) > > How can I use this character array to generate a command using rbind() > on all of these data frames at once. > > I tried to apply eval() somehow, but did not come up with the right idea. > > Thanks > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Will this do it for you:> ds1 <- data.frame(x=c(1,2,3,4), y=c(5,6,7,8)) > ds2 <- data.frame(x=c(9,10,11,12), y=c(13,14,15,16)) > ds3 <- data.frame(x=c(1,2,3,4), y=c(5,6,7,8)) > ds4 <- data.frame(x=c(9,10,11,12), y=c(13,14,15,16)) > x.n <- c('ds1','ds2','ds3','ds4') > > # create a list of data frames > x.list <- lapply(x.n, get) > > # combine into a single dataframe > do.call(rbind, x.list)x y 1 1 5 2 2 6 3 3 7 4 4 8 5 9 13 6 10 14 7 11 15 8 12 16 9 1 5 10 2 6 11 3 7 12 4 8 13 9 13 14 10 14 15 11 15 16 12 16>On 4/1/08, Hans W. Borchers <hwborchers at gmail.com> wrote:> I have a set of data frames ds1, ds2, ... each having the same columns > and column names: > > ds1 <- data.frame(x=c(1,2,3,4), y=c(5,6,7,8)) > ds1 <- data.frame(x=c(9,10,11,12), y=c(13,14,15,16)) > ... > > and I would like to combine them into just one data frame like > > ds <- rbind(ds1, ds2, ...) > > Because there are so many of them, I will have to use a character array > > nms <- c("ds1", "ds2", ...) > > How can I use this character array to generate a command using rbind() > on all of these data frames at once. > > I tried to apply eval() somehow, but did not come up with the right idea. > > Thanks > > ______________________________________________ > 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 you are trying to solve?