[R] averaging two matrices whilst ignoring missing values

Bill.Venables at csiro.au Bill.Venables at csiro.au
Tue Jul 14 09:16:38 CEST 2009


Another (also somewhat inelegant) way to do this might be as follows.

First write a small function to replace missing values by zeros:

zNA <- function(X) { X[is.na(X)] <- 0; X}

Now

testAve <- (zNA(test) + zNA(test2))/(2 - is.na(test) - is.na(test2))

If you are unlucky enough to have missing values at the same location in both matrices, then this will give not NA but NaN in the average.  If you really want NA's in such places you need one more step:

is.na(testAve[is.nan(testAve)]) <- TRUE

(which may need a little decoding as well.)
________________________________________
From: r-help-bounces at r-project.org [r-help-bounces at r-project.org] On Behalf Of Moshe Olshansky [m_olshansky at yahoo.com]
Sent: 14 July 2009 13:55
To: r-help at r-project.org; Tish Robertson
Subject: Re: [R] averaging two matrices whilst ignoring missing values

One (awkward) way to do this is:

x <- matrix(c(c(test),c(test2)),ncol=2)
y <- rowMeans(x,na.rm=TRUE)
testave <- matrix(y,nrow=nrow(test))

--- On Tue, 14/7/09, Tish Robertson <tishrobertson at hotmail.com> wrote:

> From: Tish Robertson <tishrobertson at hotmail.com>
> Subject: [R] averaging two matrices whilst ignoring missing values
> To: r-help at r-project.org
> Received: Tuesday, 14 July, 2009, 11:46 AM
>
> Hi folks,
>
>
>
> I'm trying to do something that seems like it should easy,
> but it apparently isn't.  I have two large matrices,
> both containing a number of missing values at different
> cells. I would like to average these matrices, but the NAs
> are preventing me. I get a "non-numeric argument to binary
> operator" error.  That's the first problem.
>
>
>
> test<-read.csv("test.csv",header=FALSE)
> test2<-read.csv("test2.csv",header=FALSE)
> test_ <- as.matrix(test,na.rm=T)
> test2_ <- as.matrix(test2,na.rm=T)
> testave<- (test_+test2_)/2
>
> ??
>
>
>
> So off the bat I'm doing something wrong.
>
>
>
> How would I replace the missing values in one matrix with
> the corresponding non-missing values in another?  It's
> acceptable to me if I only have one value representing the
> average for a particular coordinate.
>
>
>
> Any help would be appreciated!
>
>
>
>
>
>
>
> _________________________________________________________________
> Bing™ finds low fares by predicting when to book. Try it
> now.
>
> =WLHMTAG&crea=TXT_MTRHPG_Travel_Travel_TravelDeals_1x1
>     [[alternative HTML version deleted]]
>
>
> -----Inline Attachment Follows-----
>
> ______________________________________________
> 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.
>

______________________________________________
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.




More information about the R-help mailing list