Dear R gang, Can anyone help me sort out how to append one data frame to another while adding a factor to distinguish which was the original frame? For example, I have two frames, x and y> xexp size 1 a 10 2 b 9 3 c 10 4 d 12 5 e 11> yexp size 1 a 13 2 b 15 3 c 12 4 d 20 5 e 15 and I'd like to create a new frame that looks like exp size set 1 a 10 x 2 b 9 x 3 c 10 x 4 d 12 x 5 e 11 x 6 a 13 y 7 b 15 y 8 c 12 y 9 d 20 y 10 e 15 y I know that I can do something like> new.frame <- data.frame(c(as.vector(x$exp), as.vector(y$exp))) > new.frame$size <- c(as.vector(x$size), as.vector(y$size)) > new.frame$set <- c(rep('x',times=length(x$exp)), rep('y',times=length(y$exp))) > names(new.frame) <- c('exp','size','set')Is there any generalized code out there that will do this sort of thing for more complex data frames (still with the same structure though)? Or is there a simpler R idiom that does the same thing? Mike -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On 3 Dec 2001, Michael A. Miller wrote:> Dear R gang, > > Can anyone help me sort out how to append one data frame to > another while adding a factor to distinguish which was the > original frame? > > For example, I have two frames, x and y > > > x > exp size > 1 a 10 > 2 b 9 > 3 c 10 > 4 d 12 > 5 e 11 > > > y > exp size > 1 a 13 > 2 b 15 > 3 c 12 > 4 d 20 > 5 e 15 > > and I'd like to create a new frame that looks like > > exp size set > 1 a 10 x > 2 b 9 x > 3 c 10 x > 4 d 12 x > 5 e 11 x > 6 a 13 y > 7 b 15 y > 8 c 12 y > 9 d 20 y > 10 e 15 y >rbind(cbind(x,set=rep("x",NROW(x))), cbind(y,set=rep("y",NROW(x))) ) will do it. -thomas -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On 03 Dec 2001 14:35:54 -0500, you wrote in message <87adx06qqt.fsf at lumen.med.iupui.edu>:>Dear R gang, > >Can anyone help me sort out how to append one data frame to >another while adding a factor to distinguish which was the >original frame?As long as the names match exactly in the two original frames, you should be able to do it using "rbind" and "cbind": Specifically: rbind( cbind(x, 'x'), cbind(y, 'y') ) What the cbind's are doing is adding a column to the original dataframes giving their name. Then the rbind glues the two dataframes together. If the names don't match exactly, then you should rename things so they do. Duncan Murdoch -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> From: mmiller3 at iupui.edu [mailto:mmiller3 at iupui.edu]> > Dear R gang, > > Can anyone help me sort out how to append one data frame to > another while adding a factor to distinguish which was the > original frame? > Here is the function that I wrote to accomplish this task. It will appear in the next version of the gregmisc library (due sometime January). concat <- function(..., names=NULL) { tmp <- list(...) if(is.null(names)) names <- names(tmp) if(is.null(names)) names <- sapply( as.list(match.call()), deparse)[-1] if( any( sapply(tmp, is.matrix) | sapply(tmp, is.data.frame) ) ) { len <- sapply(tmp, function(x) c(dim(x),1)[1] ) len[is.null(len)] <- 1 data <- rbind( ... ) } else { len <- sapply(tmp,length) data <- unlist(tmp) } namelist <- factor(rep(names, len), levels=names) return( data.frame( data, source=namelist) ) } You use it like:> xexp size 1 a 10 2 b 9 3 c 10 4 d 12 5 e 11> yexp size 1 a 13 2 b 15 3 c 12 4 d 20 5 e 15> concat(x,y)exp size source 1 a 10 x 2 b 9 x 3 c 10 x 4 d 12 x 5 e 11 x 6 a 13 y 7 b 15 y 8 c 12 y 9 d 20 y 10 e 15 y LEGAL NOTICE Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
As S-PLUS has a function concat() which does something different (like c, strips names) could you use a different name? B On Mon, 3 Dec 2001, Warnes, Gregory R wrote:> > From: mmiller3 at iupui.edu [mailto:mmiller3 at iupui.edu] > > > > Dear R gang, > > > > Can anyone help me sort out how to append one data frame to > > another while adding a factor to distinguish which was the > > original frame? > > > > Here is the function that I wrote to accomplish this task. It will appear > in the next version of the gregmisc library (due sometime January). > > concat <- function(..., names=NULL) > { > tmp <- list(...) > if(is.null(names)) names <- names(tmp) > if(is.null(names)) names <- sapply( as.list(match.call()), deparse)[-1] > > if( any( > sapply(tmp, is.matrix) > | > sapply(tmp, is.data.frame) ) ) > { > len <- sapply(tmp, function(x) c(dim(x),1)[1] ) > len[is.null(len)] <- 1 > data <- rbind( ... ) > } > else > { > len <- sapply(tmp,length) > data <- unlist(tmp) > > } > > namelist <- factor(rep(names, len), levels=names) > > return( data.frame( data, source=namelist) ) > } > > You use it like: > > > x > exp size > 1 a 10 > 2 b 9 > 3 c 10 > 4 d 12 > 5 e 11 > > y > exp size > 1 a 13 > 2 b 15 > 3 c 12 > 4 d 20 > 5 e 15 > > concat(x,y) > exp size source > 1 a 10 x > 2 b 9 x > 3 c 10 x > 4 d 12 x > 5 e 11 x > 6 a 13 y > 7 b 15 y > 8 c 12 y > 9 d 20 y > 10 e 15 y > > > LEGAL NOTICE > Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately. > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
I wasn't aware of the S-plus function 'concat'. I'll rename my function 'combine' (unless someone else has a better suggestion). -Greg > -----Original Message----- > From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk] > Sent: Monday, December 03, 2001 5:42 PM > To: Warnes, Gregory R > Cc: 'mmiller3 at iupui.edu'; r-help at stat.math.ethz.ch > Subject: RE: [R] appending similar data frames? > > > As S-PLUS has a function concat() which does something > different (like c, > strips names) could you use a different name? > > B > > On Mon, 3 Dec 2001, Warnes, Gregory R wrote: > > > > From: mmiller3 at iupui.edu [mailto:mmiller3 at iupui.edu] > > > > > > Dear R gang, > > > > > > Can anyone help me sort out how to append one data frame to > > > another while adding a factor to distinguish which was the > > > original frame? > > > > > > > Here is the function that I wrote to accomplish this > task. It will appear > > in the next version of the gregmisc library (due sometime > January). > > > > concat <- function(..., names=NULL) > > { > > tmp <- list(...) > > if(is.null(names)) names <- names(tmp) > > if(is.null(names)) names <- sapply( > as.list(match.call()), deparse)[-1] > > > > if( any( > > sapply(tmp, is.matrix) > > | > > sapply(tmp, is.data.frame) ) ) > > { > > len <- sapply(tmp, function(x) c(dim(x),1)[1] ) > > len[is.null(len)] <- 1 > > data <- rbind( ... ) > > } > > else > > { > > len <- sapply(tmp,length) > > data <- unlist(tmp) > > > > } > > > > namelist <- factor(rep(names, len), levels=names) > > > > return( data.frame( data, source=namelist) ) > > } > > > > You use it like: > > > > > x > > exp size > > 1 a 10 > > 2 b 9 > > 3 c 10 > > 4 d 12 > > 5 e 11 > > > y > > exp size > > 1 a 13 > > 2 b 15 > > 3 c 12 > > 4 d 20 > > 5 e 15 > > > concat(x,y) > > exp size source > > 1 a 10 x > > 2 b 9 x > > 3 c 10 x > > 4 d 12 x > > 5 e 11 x > > 6 a 13 y > > 7 b 15 y > > 8 c 12 y > > 9 d 20 y > > 10 e 15 y > > > > > > LEGAL NOTICE > > Unless expressly stated otherwise, this message is > confidential and may be privileged. It is intended for the > addressee(s) only. Access to this E-mail by anyone else is > unauthorized. If you are not an addressee, any disclosure > or copying of the contents of this E-mail or any action > taken (or not taken) in reliance on it is unauthorized and > may be unlawful. If you are not an addressee, please inform > the sender immediately. > > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. > -.-.-.-.-.-.-.-.-.- > > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html> Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch >_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._>-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. -.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Version 1.3.1 (2001-08-31) (RH 7.2):> dat <- data.frame(x = 1, y = 2) > x <- matrix(0, ncol = 2, nrow = 2) > x[,1] [,2] [1,] 0 0 [2,] 0 0> datx y 1 1 2> rbind(dat, x)x y 1 1 2 2 0 0 I expected> rbind(dat, x)x y 1 1 2 2 0 0 3 0 0 Is my expectation wrong? G?ran Brostr?m -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._