Re: [R] use of NA's

From: Roger D. Peng <rpeng_at_jhsph.edu>
Date: Sat 06 Aug 2005 - 01:37:09 EST

To test for 'NA' you shouldn't use '=='. Use 'is.na()' instead.

-roger

tom wright wrote:
> Can someone please explain why this works:
>
>

>>>d<-c(0,2,3,2,0,3,4,0,0,0,0,0)
>>>d.mat<-matrix(data=d,nrow=4,ncol=3,byrow=TRUE)
>>>for(i in 1:length(d.mat[1,])){
>>
>>+ d.mat[,i][d.mat[,i]==0]<-mean(d.mat[,i][d.mat[,i]>0])
>>+ }

>
>
> Whereas:
>
>
>>d<-c(0,2,3,2,0,3,4,0,0,0,0,0)
>>d.mat<-matrix(data=d,nrow=4,ncol=3,byrow=TRUE)
>>d.mat[d.mat==0]<-NA
>>for(i in 1:length(d.mat[1,])){

>
> + d.mat[,i][d.mat[,i]==NA]<-mean(d.mat[,i],na.rm=TRUE)
> + }
> dosnt
>
> Thanks
> Tom
>
> ______________________________________________
> 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
>
-- 
Roger D. Peng
http://www.biostat.jhsph.edu/~rpeng/

______________________________________________
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 Sat Aug 06 01:41:59 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 15:06:08 EST