[R] Combine two matricies
Dimitris Rizopoulos
d.rizopoulos at erasmusmc.nl
Mon Jul 13 11:40:35 CEST 2009
try this:
a <- matrix(c(3,4,5,2,3,4,1,1,2), nrow=3, ncol=3)
x <- matrix(c(3, NA, NA, NA, 2, 5, NA, 2, 2), nrow=3, ncol=3)
ind <- is.na(x)
x[ind] <- a[ind]
x
I hope it helps.
Best,
Dimitris
Tom Liptrot wrote:
> Hi,
>
> I have two matricies a and x:
>
> a<-matrix(c(3,4,5,2,3,4,1,1,2), nrow=3, ncol=3)
>
> [,1] [,2] [,3]
> [1,] 3 2 1
> [2,] 4 3 1
> [3,] 5 4 2
>
> x<-matrix(c(3, NA, NA, NA, 2, 5, NA, 2, 2), nrow=3, ncol=3)
>
> [,1] [,2] [,3]
> [1,] 3 NA NA
> [2,] NA 2 2
> [3,] NA 5 2
>
> I wish to combine these two into one matrix using the values from x where x has values, and values from a where x has NA's, giving a new matrix which would look like this:
>
> ax<-matrix(c(3,4,5,2,2,5,1,2,2), nrow=3, ncol=3)
>
> [,1] [,2] [,3]
> [1,] 3 2 1
> [2,] 4 2 2
> [3,] 5 5 2
>
> I want an automatic way of doing this as my actual application is a much larger matrix.
>
> Thanks in advance
>
> Tom
>
>
> _________________________________________________________________
>
> [[elided Hotmail spam]]
>
>
> [[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.
>
--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center
Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
More information about the R-help
mailing list