I have a program, when I write if(num!=NA) it yields an error message. However, if I write if(is.na(num)==FALSE) it works. Why doesn't the first statement work? Thanks, Miao [[alternative HTML version deleted]]
You can use only if(!is.na(num)) [[alternative HTML version deleted]]
A logical operation involving NA returns NA, never TRUE or FALSE: See the 8th Circle of the R Inferno (8.1.4): http://www.burns-stat.com/pages/Tutor/R_inferno.pdf> num <- 1 > num==NA[1] NA> is.na(num)[1] FALSE ------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of jpm miao Sent: Friday, May 3, 2013 10:25 AM To: r-help Subject: [R] Why can't R understand if(num!=NA)? I have a program, when I write if(num!=NA) it yields an error message. However, if I write if(is.na(num)==FALSE) it works. Why doesn't the first statement work? Thanks, Miao [[alternative HTML version deleted]] ______________________________________________ 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.
> -----Original Message----- > if(num!=NA) > it yields an error message.> Why doesn't the first statement work?Because you just compared something with NA (usually interpreted as 'missing') and because of that the comparison result is also NA. 'if' then tells you that you have a missing value where you need either TRUE or FALSE. Play with num!=NA #returns NA and if(NA) "Not there" #returns error is.na() returns TRUE for NA's, so 'if' knows what to do with the answer. S Ellison ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}
On May 3, 2013, at 8:24 AM, jpm miao wrote:> I have a program, when I write > > if(num!=NA) > > it yields an error message. > > However, if I write > > if(is.na(num)==FALSE) > > it works. > > Why doesn't the first statement work?Read the "manual": ?"NA" -- David Winsemius Alameda, CA, USA
On May 3, 2013, at 10:24 AM, jpm miao <miaojpm at gmail.com> wrote:> I have a program, when I write > > if(num!=NA) > > it yields an error message. > > However, if I write > > if(is.na(num)==FALSE) > > it works. > > Why doesn't the first statement work? > > Thanks, > > MiaoNA is undefined:> NA == NA[1] NA> NA != NA[1] NA Therefore the equality you are attempting does not return a TRUE or FALSE result, it is unknown and NA is returned. ?is.na was designed specifically to test for the presence of an NA value and return a TRUE or FALSE result which can then be tested. See: http://cran.r-project.org/doc/manuals/r-release/R-intro.html#Missing-values Regards, Marc Schwartz
> if(num!=NA) > Why doesn't the first statement work?An NA value means that the value is unknown. E.g., age <- NA means the you do not know the age of your subject. (The subject has an age, NA means you did not collect that data.) Thus you do not know the value of age == 6 either, the subject might be 6 or it might not be. Hence R makes the value of age==6 NA. Since R does not have different evaluation rules for literal values and expressions that means that NA==6 and NA==someAge must evaluate to NA as well. The second part of the question is why if (NA) { } else { } causes an error. It is a bit arbitrary, but there is a mismatch between a 2-way 'if' statement and 3-valued logical data and R deals with it by insisting that the condition in if (condition) { } else {} be either TRUE or FALSE, not NA. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of jpm miao > Sent: Friday, May 03, 2013 8:25 AM > To: r-help > Subject: [R] Why can't R understand if(num!=NA)? > > I have a program, when I write > > if(num!=NA) > > it yields an error message. > > However, if I write > > if(is.na(num)==FALSE) > > it works. > > Why doesn't the first statement work? > > Thanks, > > Miao > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
At a minimum, the first statement needs "==". Also, is.na() gives TRUE/FALSE. While a logical comparison to NA gives NA as a value. Kevin On Fri, May 3, 2013 at 10:24 AM, jpm miao <miaojpm@gmail.com> wrote:> I have a program, when I write > > if(num!=NA) > > it yields an error message. > > However, if I write > > if(is.na(num)==FALSE) > > it works. > > Why doesn't the first statement work? > > Thanks, > > Miao > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >-- Kevin Wright [[alternative HTML version deleted]]
On 03-05-2013, at 17:24, jpm miao <miaojpm at gmail.com> wrote:> I have a program, when I write > > if(num!=NA) > > it yields an error message. >it? What is unclear about the error message?> However, if I write > > if(is.na(num)==FALSE) > > it works. > > Why doesn't the first statement work? >Read section 2.5 'Missing values" of the manual "An Introduction to R". Berend> Thanks, > > Miao > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
?num1<- c(0,NA,1,3) ?num1==NA #[1] NA NA NA NA ?num1!=NA #[1] NA NA NA NA ?is.na(num1) #[1] FALSE? TRUE FALSE FALSE A.K. ----- Original Message ----- From: jpm miao <miaojpm at gmail.com> To: r-help <r-help at r-project.org> Cc: Sent: Friday, May 3, 2013 11:24 AM Subject: [R] Why can't R understand if(num!=NA)? I have a program, when I write if(num!=NA) it yields an error message. However, if I write if(is.na(num)==FALSE) it works. Why doesn't the first statement work? Thanks, Miao ??? [[alternative HTML version deleted]] ______________________________________________ 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.
On May 3, 2013, at 17:24 , jpm miao wrote:> I have a program, when I write > > if(num!=NA) > > it yields an error message. > > However, if I write > > if(is.na(num)==FALSE) > > it works. > > Why doesn't the first statement work?Because comparison with an unknown value yields an unknown result. By the way, comparing a logical value to FALSE is silly: if ( !is.na(num) ) will do it. -- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Seemingly Similar Threads
- Definition of "lag" is opposite in ts and xts objects!
- How can I tabulate time series data (in RStudio or any other R editor)?
- How to print the frequency table (produced by the command "table" to Excel
- How can we access an element in a structure
- Debugging using RStudio or any other R editor