# Re: [R] use of NA's

From: Hank Stevens <HStevens_at_muohio.edu>
Date: Sat 15 Oct 2005 - 21:35:29 EST

Hi Tom,
You need to use the is.na test rather then the test x==NA because the latter is not defined.

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][is.na(d.mat[,i])] <- mean(d.mat[,i],na.rm=TRUE) }

Hank

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