Re: [R] Why can't R understand if(num!=NA)?

2013-05-04 Thread peter dalgaard

On May 3, 2013, at 21:36 , David Winsemius wrote:

 
 On May 3, 2013, at 10:46 AM, peter dalgaard wrote:
 
 
 Because comparison with an unknown value yields an unknown result. 
 
 Anything else would violate the Second Law of Thermodynamics. We cannot have 
 comparisons reducing entropy, now can we? Uncertainty cannot run uphill.

Now what does this say about SAS, where the missing value is smaller than all 
regular numbers? I.e.,

DATA;
  iteen = (age  20);

turns people of unknown age into instant teenagers.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd@cbs.dk  Priv: pda...@gmail.com

__
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.


Re: [R] Why can't R understand if(num!=NA)?

2013-05-04 Thread David Winsemius

On May 3, 2013, at 10:58 PM, peter dalgaard wrote:

 
 On May 3, 2013, at 21:36 , David Winsemius wrote:
 
 
 On May 3, 2013, at 10:46 AM, peter dalgaard wrote:
 
 
 Because comparison with an unknown value yields an unknown result. 
 
 Anything else would violate the Second Law of Thermodynamics. We cannot have 
 comparisons reducing entropy, now can we? Uncertainty cannot run uphill.
 
 Now what does this say about SAS, where the missing value is smaller than all 
 regular numbers? I.e.,
 
 DATA;
  iteen = (age  20);
 
 turns people of unknown age into instant teenagers.

And are handled as zero when included in calculations using SUM ( as, I also 
read, does SPSS). 

So SAS comparisons are still increasing entropy. Quantum mechanics says there 
is no real vacuum state, so maybe that's where those not-really-missing missing 
values are ending up after they confound our notions of existence.

-- 
David Winsemius
Alameda, CA, USA

__
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.


[R] Why can't R understand if(num!=NA)?

2013-05-03 Thread jpm miao
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.


Re: [R] Why can't R understand if(num!=NA)?

2013-05-03 Thread S Ellison
 

 -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}}

__
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.


Re: [R] Why can't R understand if(num!=NA)?

2013-05-03 Thread Leandro Marino
You can use only

if(!is.na(num))

[[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.


Re: [R] Why can't R understand if(num!=NA)?

2013-05-03 Thread David Carlson
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 AM University
College Station, TX 77840-4352


-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@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@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.

__
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.


Re: [R] Why can't R understand if(num!=NA)?

2013-05-03 Thread Marc Schwartz

On May 3, 2013, at 10:24 AM, jpm miao miao...@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


NA 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

__
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.


Re: [R] Why can't R understand if(num!=NA)?

2013-05-03 Thread David Winsemius

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

__
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.


Re: [R] Why can't R understand if(num!=NA)?

2013-05-03 Thread Berend Hasselman

On 03-05-2013, at 17:24, jpm miao miao...@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@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.

__
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.


Re: [R] Why can't R understand if(num!=NA)?

2013-05-03 Thread arun
 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 miao...@gmail.com
To: r-help r-help@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@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.


__
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.


Re: [R] Why can't R understand if(num!=NA)?

2013-05-03 Thread William Dunlap
 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-boun...@r-project.org [mailto:r-help-boun...@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@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.

__
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.


Re: [R] Why can't R understand if(num!=NA)?

2013-05-03 Thread Kevin Wright
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 miao...@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]]

__
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.


Re: [R] Why can't R understand if(num!=NA)?

2013-05-03 Thread peter dalgaard

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@cbs.dk  Priv: pda...@gmail.com

__
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.


Re: [R] Why can't R understand if(num!=NA)?

2013-05-03 Thread David Winsemius



 
 On May 3, 2013, at 17:24 , jpm miao wrote:
 
 I have a program, when I write
 
 if(num!=NA)
 
 snipped

On May 3, 2013, at 10:46 AM, peter dalgaard wrote:

 Because comparison with an unknown value yields an unknown result. 

Anything else would violate the Second Law of Thermodynamics. We cannot have 
comparisons reducing entropy, now can we? Uncertainty cannot run uphill.

-- 

David Winsemius
Alameda, CA, USA

__
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.


Re: [R] Why can't R understand if(num!=NA)?

2013-05-03 Thread David Winsemius

On May 3, 2013, at 10:46 AM, peter dalgaard wrote:

 
 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. 

Anything else would violate the Second Law of Thermodynamics. We cannot have 
comparisons reducing entropy, now can we? Uncertainty cannot run uphill.

-- 
David Winsemius
Alameda, CA, USA

__
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.