Haynes, Maurice (NIH/NICHD)
2002-Nov-27 14:39 UTC
[R] Error message: missing value where logical needed
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 O. Maurice Haynes, Applied Statistician National Institute of Child Health and Human Development Child and Family Research Section 6705 Rockledge Drive Bethesda, MD 20892 Voice: 301-496-8180 Fax: 301-496-2766 E-Mail: mh192j at nih.gov -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
"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, > > MauriceSee ?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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
I think you need something more like MXPLOCO[is.na(MXPLOCO[i]) | MXPLOCI[i] == -9] <- NA which will be faster (since it's vectorized rather than using a for loop) as well as not giving you an error. The basic problem is that x==NA gives NA instead of TRUE or FALSE (since *any* operation with an NA gets "tainted" by the missingness of that datum). is.na() is the way to go. You could also look at the na.strings argument of read.table(). Ben Bolker On Wed, 27 Nov 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 > > O. Maurice Haynes, Applied Statistician > National Institute of Child Health and Human Development > Child and Family Research Section > 6705 Rockledge Drive > Bethesda, MD 20892 > Voice: 301-496-8180 > Fax: 301-496-2766 > E-Mail: mh192j at nih.gov > > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-- 318 Carr Hall bolker at zoo.ufl.edu Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker Box 118525 (ph) 352-392-5697 Gainesville, FL 32611-8525 (fax) 352-392-3704 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._