[R] Error message: missing value where logical needed

Uwe Ligges ligges at statistik.uni-dortmund.de
Wed Nov 27 16:53:24 CET 2002



"Haynes, Maurice (NIH/NICHD)" wrote:
> 
> Dear all,
> 
> I often import data sets from other programs that contain user defined
> missing values.  As an example consider a variable MXPLOCO with a user
> defined missing value of as -9.
> 
> > summary(MXPLOCO)
>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's
>  -9.000   4.750   5.000   5.349   6.000   8.500   2.000
> 
> The following command successfully converts the value -9 to NA.  However, an
> error message is also returned:
> 
> > for(i in 1:length(MXPLOCO)) {
> +     if(MXPLOCO[i] == c(-9, NA)) MXPLOCO[i] <- NA
> +     }
> Error in if (MXPLOCO[i] == c(-9, NA)) MXPLOCO[i] <- NA :
>         missing value where logical needed
> > summary(MXPLOCO)
>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's
>    4.00    5.00    5.00    5.54    6.00    8.50    3.00
> 
> What is the meaning of the error message, and how can I resolve the error?
> 
> Thanks,
> 
> Maurice
 


See ?NA on how to deal with NAs (specifically test on NAs [is.na()] and
assignment of NAs [as well is.na(), in principle]). Examples:
 is.na(x)
 is.na(x) <- TRUE

You can specify the NA character in read.table at once as in:
 read.table(my.file, na.string = -9)

Uwe Ligges
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list