[R] how to fill out the empty spots when using rbind or cbind?

jim holtman jholtman at gmail.com
Sat Oct 3 02:23:21 CEST 2009


I will take a guess at what you want since you did not program an
example of your data or output desired:

> a <- 1:4
> b <- 1:6
> # get error message
> cbind(a,b)
     a b
[1,] 1 1
[2,] 2 2
[3,] 3 3
[4,] 4 4
[5,] 1 5
[6,] 2 6
Warning message:
In cbind(a, b) :
  number of rows of result is not a multiple of vector length (arg 1)
> # now combine them filling out with NAs
> combine <- list(a, b)  # create a list of vectors to be bound
> max.length <- max(sapply(combine, length))
> do.call(cbind, lapply(combine, function(.vec){
+     c(.vec, rep(NA, max.length - length(.vec)))
+ }))
     [,1] [,2]
[1,]    1    1
[2,]    2    2
[3,]    3    3
[4,]    4    4
[5,]   NA    5
[6,]   NA    6


On Fri, Oct 2, 2009 at 6:12 PM,  <tkedch at msn.com> wrote:
>
> I have uneven vectors I want to use cbind or rbind to combine them into a matrix. Is there a way to make it so that R would not return error msg saying they're uneven?
> Thanks.
>
> Edward Chen
> Email: tkedch at msn.com
> Cell Phone: 510-371-4717
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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 that you are trying to solve?




More information about the R-help mailing list