Tish Robertson
2009-Jul-14 01:46 UTC
[R] averaging two matrices whilst ignoring missing values
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]]
Rolf Turner
2009-Jul-14 03:44 UTC
[R] averaging two matrices whilst ignoring missing values
On 14/07/2009, at 1:46 PM, Tish Robertson wrote:> 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.This error has nothing to do with NAs. There's something else wrong with your data. ***Look*** at them!!!> 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)What on earth do you expect to accomplish by the two foregoing calls? There is no na.rm argument to as.matrix() and even if there were, what the hell did you expect to get? What did you expect the dimension of the results to be? If NAs were removed, how could things possibly be arranged in order that the following would make any sense at all?> testave<- (test_+test2_)/2 > > ??Indeed.> So off the bat I'm doing something wrong.That's for sure. Did you ***look*** at ``test_'' and ``test2_''?> 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!The simplest thing to do would be testave <- apply(cbind(as.vector(test),as.vector(test2)), 1,mean,na.rm=TRUE) testave <- matrix(testave,nrow=nrow(test),ncol=ncol(test)) Slightly sexier: Install and load the package abind; then testave <- apply(abind(test,test2,along=3),c(1,2),mean,na.rm=TRUE) Note that mean() ***does*** have an na.rm argument!!! cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
Moshe Olshansky
2009-Jul-14 03:55 UTC
[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. >