[R] rbind-ing numeric matrices
Sarah Goslee
sarah.goslee at gmail.com
Tue May 1 20:32:42 CEST 2012
On Tue, May 1, 2012 at 2:12 PM, Nick Switanek <nswitanek at gmail.com> wrote:
> Thank you, Sarah!
>
> Yes, unlist() looks like it will do just what I need. Thank you.
>
> I took a different tack. I'd changed the function returning A to coerce the
> row from the data.frame using as.numeric(). Any reason to prefer your way or
> mine?
Possibly, but we would need to know what you did to produce A to be certain.
Sarah
> thanks again,
> Nick
>
>
> On Tue, May 1, 2012 at 2:08 PM, Sarah Goslee <sarah.goslee at gmail.com> wrote:
>>
>> Hi Nick,
>>
>> On Tue, May 1, 2012 at 1:56 PM, Nick Switanek <nswitanek at gmail.com> wrote:
>> > Thank you, Steve and Sarah, for your swift replies.
>> >
>> > I didn't know about dput(). class() returns "matrix" for A, B, and C,
>> > but
>> > here:
>> >
>> >> class(A)
>> > [1] "matrix"
>> >> class(B)
>> > [1] "matrix"
>> >> dput(A)
>> > structure(list(1239814462, 1239814601, 14349, 3, 4, 0, 12, 46601,
>> > 17801, 12401, 106001), .Dim = c(1L, 11L), .Dimnames = list(
>> > NULL, c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
>> > "k")))
>>
>> I'm not sure how you created A, but it's a list - see the structure
>> given by dput(), and also:
>>
>> > is.list(A)
>> [1] TRUE
>> > is.list(B)
>> [1] FALSE
>>
>>
>> >> dput(B)
>> > structure(c(1239197400, 1239199200, 1239202800, 1239199199, 1239202799,
>> > 1239206399, 14342, 14342, 14342, 3, 3, 3, 0, 0, 0, 0, 0, 0, 9,
>> > 10, 11, 35999, 39599, 43199, 7199, 10799, 14399, 1799, 5399,
>> > 8999, 1799, 5399, 8999), .Dim = c(3L, 11L), .Dimnames = list(
>> > NULL, c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
>> > "k")))
>>
>> Note that the structure for B doesn't say anything about it being a list.
>>
>> In this particular case, you can fix that with:
>> > rbind(unlist(A), B)
>> a b c d e f g h i j k
>> [1,] 1239814462 1239814601 14349 3 4 0 12 46601 17801 12401 106001
>> [2,] 1239197400 1239199199 14342 3 0 0 9 35999 7199 1799 1799
>> [3,] 1239199200 1239202799 14342 3 0 0 10 39599 10799 5399 5399
>> [4,] 1239202800 1239206399 14342 3 0 0 11 43199 14399 8999 8999
>>
>> But now I'm curious where A came from.
>>
>> Sarah
>>
>> >> C <- rbind(A,B)
>> >> dput(C)
>> > structure(list(1239814462, 1239197400, NULL, NULL, 1239814601,
>> > 1239199200, NULL, NULL, 14349, 1239202800, NULL, NULL, 3,
>> > 1239199199, NULL, NULL, 4, 1239202799, NULL, NULL, 0, 1239206399,
>> > NULL, NULL, 12, 14342, NULL, NULL, 46601, 14342, NULL, NULL,
>> > 17801, 14342, NULL, NULL, 12401, 3, NULL, NULL, 106001, 3,
>> > NULL, NULL), .Dim = c(4L, 11L), .Dimnames = list(NULL, c("a",
>> > "b", "c", "d", "e", "f", "g", "h", "i", "j", "k")))
>> >
>> > But how do I convert A to the appropriate form for rbinding? Not simply
>> > with as.matrix(). Witness:
>> >> dput(as.matrix(A))
>> > structure(list(1239814462, 1239814601, 14349, 3, 4, 0, 12, 46601,
>> > 17801, 12401, 106001), .Dim = c(1L, 11L), .Dimnames = list(
>> > NULL, c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
>> > "k")))
>> >
>> > I think we're closer but I still need your help!
>> >
>> > Nick
>> >
>> >
>> > On Tue, May 1, 2012 at 12:02 PM, Steve Lianoglou <
>> > mailinglist.honeypot at gmail.com> wrote:
>> >
>> >> 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
>> >>
>>
>>
>> --
>> Sarah Goslee
>> http://www.functionaldiversity.org
>
>
--
Sarah Goslee
http://www.stringpage.com
http://www.sarahgoslee.com
http://www.functionaldiversity.org
More information about the R-help
mailing list