[R] merge numerous columns of unequal length

markleeds at verizon.net markleeds at verizon.net
Tue May 6 01:02:15 CEST 2008


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.



More information about the R-help mailing list