Difficulty handling NA's: Assume that I have a numeric vector y. For simplicity, assume that it has 10 elements. Assume that the third element has the value NA. I give it the following: NA_test <- function (){ y <- numeric (10) y [3] <- NA if (y [3] != NA){(print ("no")} print ("Leaving NA_test") return () }# End of function ------------------------------------------------------------- Unfortunately, things become confused involving the NA element. Here is the output, starting with the loading process: ----------------------------------------------------------> NA_test <- function (){+ y <- numeric (10) + y [3] <- NA + if (y [3] != NA){(print ("no")} Error: syntax error in: "y [3] <- NA if (y [3] != NA){(print ("no")}"> print ("Leaving NA_test")[1] "Leaving NA_test"> return ()Error: no function to return from, jumping to top level> }# End of functionError: syntax error in "}">--------------------------------------------------------------------- I have enclosed the print operation in braces to avoid possible problems with it. Your advice? Tom Jones
On Mon, 2007-11-19 at 22:18 -0500, Thomas L Jones, PhD wrote:> Difficulty handling NA's: > Assume that I have a numeric vector y. For simplicity, assume that it has 10 > elements. Assume that the third element has the value NA. I give it the > following: > NA_test <- function (){ > y <- numeric (10) > y [3] <- NA > if (y [3] != NA){(print ("no")} > print ("Leaving NA_test") > return () > }# End of function > > ------------------------------------------------------------- > Unfortunately, things become confused involving the NA element. > Here is the output, starting with the loading process: > > ---------------------------------------------------------- > > NA_test <- function (){ > + y <- numeric (10) > + y [3] <- NA > + if (y [3] != NA){(print ("no")} > Error: syntax error in: > "y [3] <- NA > if (y [3] != NA){(print ("no")}" > > print ("Leaving NA_test") > [1] "Leaving NA_test" > > return () > Error: no function to return from, jumping to top level > > }# End of function > Error: syntax error in "}" > > > --------------------------------------------------------------------- > I have enclosed the print operation in braces to avoid possible problems > with it. > > Your advice? > > Tom JonesSince NA is an undefined value by definition, you cannot use [in]equalities to test for its presence or absence. Thus: 1. To test if an element in a vector is NA, use: is.na(Vec) 2. To set one or more elements in a vector to NA use: is.na(Vec) <- Indices So: y <- numeric(10)> y[1] 0 0 0 0 0 0 0 0 0 0 is.na(y) <- 3> y[1] 0 0 NA 0 0 0 0 0 0 0> is.na(y)[1] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE> is.na(y[3])[1] TRUE See ?NA for more information. HTH, Marc Schwartz
The problem is that if you do the following: x <- NA then x == NA returns NA (and not TRUE or even FALSE). Use is.na to test for NA (see ?is.na). --- "Thomas L Jones, PhD" <jones3745 at verizon.net> wrote:> Difficulty handling NA's: > Assume that I have a numeric vector y. For > simplicity, assume that it has 10 > elements. Assume that the third element has the > value NA. I give it the > following: > NA_test <- function (){ > y <- numeric (10) > y [3] <- NA > if (y [3] != NA){(print ("no")} > print ("Leaving NA_test") > return () > }# End of function > >-------------------------------------------------------------> Unfortunately, things become confused involving the > NA element. > Here is the output, starting with the loading > process: > >----------------------------------------------------------> > NA_test <- function (){ > + y <- numeric (10) > + y [3] <- NA > + if (y [3] != NA){(print ("no")} > Error: syntax error in: > "y [3] <- NA > if (y [3] != NA){(print ("no")}" > > print ("Leaving NA_test") > [1] "Leaving NA_test" > > return () > Error: no function to return from, jumping to top > level > > }# End of function > Error: syntax error in "}" > > >---------------------------------------------------------------------> I have enclosed the print operation in braces to > avoid possible problems > with it. > > Your advice? > > Tom Jones > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, > reproducible code. >
Thomas L Jones, PhD wrote:> Difficulty handling NA's: > Assume that I have a numeric vector y. For simplicity, assume that it has 10 > elements. Assume that the third element has the value NA. I give it the > following: > NA_test <- function (){ > y <- numeric (10) > y [3] <- NA > if (y [3] != NA){(print ("no")} > print ("Leaving NA_test") > return () > }# End of function > > ------------------------------------------------------------- > Unfortunately, things become confused involving the NA element. > Here is the output, starting with the loading process: > > ---------------------------------------------------------- >> NA_test <- function (){ > + y <- numeric (10) > + y [3] <- NA > + if (y [3] != NA){(print ("no")} > Error: syntax error in: > "y [3] <- NA > if (y [3] != NA){(print ("no")}" >> print ("Leaving NA_test") > [1] "Leaving NA_test" >> return () > Error: no function to return from, jumping to top level >> }# End of function > Error: syntax error in "}" > --------------------------------------------------------------------- > I have enclosed the print operation in braces to avoid possible problems > with it. > > Your advice? > > Tom JonesWhat Messrs. Schwartz and Olshansky told you is valid, but will not cure syntax errors. My advice is to check the matching of parentheses in the line > if (y [3] != NA){(print ("no")} J. R. M. Hosking
somewhere I read that " !is.na(your_vector)" is better than "your_vector!=NA" . Thomas L Jones, PhD wrote:> > Difficulty handling NA's: > Assume that I have a numeric vector y. For simplicity, assume that it has > 10 > elements. Assume that the third element has the value NA. I give it the > following: > NA_test <- function (){ > y <- numeric (10) > y [3] <- NA > if (y [3] != NA){(print ("no")} > print ("Leaving NA_test") > return () > }# End of function > > ------------------------------------------------------------- > Unfortunately, things become confused involving the NA element. > Here is the output, starting with the loading process: > > ---------------------------------------------------------- >> NA_test <- function (){ > + y <- numeric (10) > + y [3] <- NA > + if (y [3] != NA){(print ("no")} > Error: syntax error in: > "y [3] <- NA > if (y [3] != NA){(print ("no")}" >> print ("Leaving NA_test") > [1] "Leaving NA_test" >> return () > Error: no function to return from, jumping to top level >> }# End of function > Error: syntax error in "}" >> > --------------------------------------------------------------------- > I have enclosed the print operation in braces to avoid possible problems > with it. > > Your advice? > > Tom Jones > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- View this message in context: http://www.nabble.com/Problems-with-NA%27s-tf4840964.html#a13872489 Sent from the R help mailing list archive at Nabble.com.