[R] use of NA's

Kjetil Brinchmann Halvorsen kjetil at acelerate.com
Sat Aug 6 01:26:30 CEST 2005


Robert Kinley wrote:

>with NAs it's always safest to use the construction 
>
>if(is.na(foo))
>
>rather than
>
>if(foo==NA)
>
>cheers          Bob Kinley
>
>
>  
>
And if it is not clear why, try:

 > NA == NA
[1] NA
 >
 Kjetil


>tom wright <tom at maladmin.com> 
>Sent by: r-help-bounces at stat.math.ethz.ch
>05/08/2005 12:30
>
>To
>r-help at stat.math.ethz.ch
>cc
>
>Subject
>[R] use of NA's
>
>
>
>
>
>
>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 at 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
>
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at 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
>
>
>
>  
>


-- 

Kjetil Halvorsen.

Peace is the most effective weapon of mass construction.
               --  Mahdi Elmandjra





-- 
Internal Virus Database is out-of-date.
Checked by AVG Anti-Virus.




More information about the R-help mailing list