tkedch at msn.com
2009-Oct-02 22:12 UTC
[R] how to fill out the empty spots when using rbind or cbind?
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@msn.com Cell Phone: 510-371-4717 [[alternative HTML version deleted]]
Daniel Malter
2009-Oct-03 00:19 UTC
[R] how to fill out the empty spots when using rbind or cbind?
Fill the respective "empty" spots in the vectors with NAs. Daniel ------------------------- cuncta stricte discussurus ------------------------- -----Urspr?ngliche Nachricht----- Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im Auftrag von tkedch at msn.com Gesendet: Friday, October 02, 2009 6:13 PM An: R help Betreff: [R] how to fill out the empty spots when using rbind or cbind? 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
2009-Oct-03 00:23 UTC
[R] how to fill out the empty spots when using rbind or cbind?
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?