I have numerous objects, each containing continuous data representing the same variable, movement rate, yet each having a different number of rows. e.g. d1<-as.matrix(rnorm(5)) d2<-as.matrix(rnorm(3)) d3<-as.matrix(rnorm(6)) How can I merge these three columns side-by-side in order to create a table regardless of the difference in length? I wish to analyze the output in a spreadsheet format. Thanks! Tyler -- View this message in context: http://www.nabble.com/merge-numerous-columns-of-unequal-length-tp17071464p17071464.html Sent from the R help mailing list archive at Nabble.com.
markleeds at verizon.net
2008-May-05 23:02 UTC
[R] merge numerous columns of unequal length
you can do below but there's no way of getting arounf having NAs in your final matrix. d1<-as.matrix(rnorm(5)) d2<-as.matrix(rnorm(3)) d3<-as.matrix(rnorm(6)) templist <- list(d1,d2,d3) maxnum <- max(sapply(templist,length)) print(maxnum) temp <- lapply(templist,function(.mat) { if (nrow(.mat) < maxnum) { return(c(.mat,rep(NA,maxnum-nrow(.mat)))) } else { return(.mat) } }) finalmat <- as.matrix(do.call(cbind,temp)) print(finalmat) On Mon, May 5, 2008 at 6:46 PM, T.D.Rudolph wrote:> I have numerous objects, each containing continuous data representing > the > same variable, movement rate, yet each having a different number of > rows. e.g. > d1<-as.matrix(rnorm(5)) > d2<-as.matrix(rnorm(3)) > d3<-as.matrix(rnorm(6)) > > How can I merge these three columns side-by-side in order to create a > table > regardless of the difference in length? I wish to analyze the output > in a > spreadsheet format. > > Thanks! > Tyler > -- > View this message in context: > http://www.nabble.com/merge-numerous-columns-of-unequal-length-tp17071464p17071464.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.
Try this also: sapply(list(d1, d2, d3), "[", 1:max(sapply(list(d1, d2, d3), length))) On Mon, May 5, 2008 at 7:46 PM, T.D.Rudolph <prairie.picker@gmail.com> wrote:> > I have numerous objects, each containing continuous data representing the > same variable, movement rate, yet each having a different number of rows. > e.g. > d1<-as.matrix(rnorm(5)) > d2<-as.matrix(rnorm(3)) > d3<-as.matrix(rnorm(6)) > > How can I merge these three columns side-by-side in order to create a > table > regardless of the difference in length? I wish to analyze the output in a > spreadsheet format. > > Thanks! > Tyler > -- > View this message in context: > http://www.nabble.com/merge-numerous-columns-of-unequal-length-tp17071464p17071464.html > Sent from the R help mailing list archive at Nabble.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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
As the answers you've received suggest, you can use a list. Or you could have two vectors: one with the data, the other with the group identity. The latter format is likely more convenient for a lot of analyses. Since your data are not inherently rectangular, it is probably best to get the idea of spreadsheet out of your head. (It is probably best anyway.) Patrick Burns patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") T.D.Rudolph wrote:> I have numerous objects, each containing continuous data representing the > same variable, movement rate, yet each having a different number of rows. > e.g. > d1<-as.matrix(rnorm(5)) > d2<-as.matrix(rnorm(3)) > d3<-as.matrix(rnorm(6)) > > How can I merge these three columns side-by-side in order to create a table > regardless of the difference in length? I wish to analyze the output in a > spreadsheet format. > > Thanks! > Tyler >