Re: [R] merge large matrices

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Fri 03 Jun 2005 - 06:29:14 EST

On 6/2/2005 4:19 PM, Stefan Mischke wrote:
> Dear List
>
> I have two large matrices A and B. Both have the same dimensions, let's
> say 20k x 30k. About half the cells of B are missing. Now I'm looking
> for an efficient way to merge them, so that the missing values in B are
> replaced by the corresponding values of A.
>
> Matrix A
> [,1] [,2] [,3]
> [1,] 1 2 3
> [2,] 4 5 6
>
> merged with Matrix B
>
> [,1] [,2] [,3]
> [1,] 10 NA NA
> [2,] NA 50 60
>
> equals
>
> [,1] [,2] [,3]
> [1,] 10 2 3
> [2,] 4 50 60
>
> One way to do this, is of course looping through all the cells,
> checking for NAs and then replacing them with the corresponding values.
> But this is way too slow for my application. There must be a more
> efficient way.
> Does R provide any functions for this?

As long as the dimensions are the same:

replace <- is.na(B)
B[replace] <- A[replace]

should work.

Duncan Murdoch



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Jun 03 06:42:27 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:22 EST