[R] rbind-ing numeric matrices

Steve Lianoglou mailinglist.honeypot at gmail.com
Tue May 1 18:02:14 CEST 2012


Hi,

On Tue, May 1, 2012 at 11:52 AM, Nick Switanek <nswitanek at gmail.com> wrote:
> Good morning,
>
> I'm running into trouble rbind-ing numeric matrices with differing numbers
> of rows. In particular, there seem to be issues whenever a one-row numeric
> matrix is involved.
>
> Assume A is a numeric matrix with 1 row and Y columns and B is a numeric
> matrix with X rows and Y columns. Let C be the result of rbinding A and B.
> Then C is a numeric matrix with X + 1 rows and Y columns, only instead of
> the rows of B being "stacked" beneath the row of A as expected, the first Y
> elements of the 1st column of B are placed in the 2nd row of C, the
> remaining values of B are discarded, and NULL values fill out the rest of
> the matrix C.
>
> The number of columns of A and B match. The colnames of A and B match. Both
> are numeric matrices. I've pored over the rbind/cbind documentation but
> can't identify why I'm getting this behavior from rbind. I'd be extremely
> grateful for your suggestions or thoughts.

If everything you say is true (and I'm understanding what you're
saying), there must be something else going on with your data.
Consider:

R> m1 <- matrix(-(1:5), nrow=1)
R> m2 <- matrix(1:20, ncol=5)
R> rbind(m1, m2)
     [,1] [,2] [,3] [,4] [,5]
[1,]   -1   -2   -3   -4   -5
[2,]    1    5    9   13   17
[3,]    2    6   10   14   18
[4,]    3    7   11   15   19
[5,]    4    8   12   16   20

Can you provide a small example of your data that reproduces the
problem you're seeing?

Construct these objects in your workspace and copy/paste the output of
dput on your m1 and m2 matrices so we can easily work w/ them.

Cheers,
-steve

-- 
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact



More information about the R-help mailing list