Re: [R] combining matrices

From: Peter Ehlers <ehlers_at_ucalgary.ca>
Date: Thu, 21 Apr 2011 08:45:20 -0700

On 2011-04-21 07:03, Marten Winter wrote:
> Heja,
>
> I hope someone is still there to help me:
>
> How can I somehow merge/combine matrices to get such a result:
>
> Matrix A
>
> A B
> x1 1 0
> x2 1 1
>
> Matrix B
>
> C D
> x3 1 0
> x4 0 1
>
> Resulting Matrix?
>
>
> A B C D
> x1 1 0 0 0
> x2 1 1 0 0
> x3 0 0 1 0
> x4 0 0 0 1
>

If you don't need this very often or very general, then use

  mat <- diag(0,4)
  mat[1:2, 1:2] <- A
  mat[3:4, 3:4] <- B
  mat

If you do need more flexibility, you could use the 'assist' package's function bdiag() which handles block-diagonal matrices:

  require(assist)
  mat <- bdiag(list(A,B))

Peter Ehlers

>
> Does anyone see this probably obvious solution with R how to do this?
> Sorry if this question is silly...it's too hot here to think properly ;O)
>
> Thanks!
> cheers
> Marten
>



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. Received on Thu 21 Apr 2011 - 15:54:35 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 21 Apr 2011 - 16:00:32 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive