[R] Why doesn't this work ?
(Ted Harding)
ted.harding at wlandres.net
Thu Mar 17 11:23:52 CET 2011
And, just to make it really clear (I hope!):
Your original expression
z <-ifelse(t==1 || 2 || 3, 1,0)
looks like a transcription into "R" of the words
"If t equals 1 or 2 or 3 then z is 1 else z is 0"
However, your "t==1 || 2 || 3" has to be parsed in the correct
order according to operator precedence. If you look at '?Syntax'
you will see that operator '==' has precedence over the "or"
operator '||'. Hence the expression "t==1 || 2 || 3" will be
parsed as
(t==1) || 2 || 3
So, whatever the value of 't' ("t==1" may be either TRUE or
FALSE), the result will be either
TRUE || 2 || 3
or
FALSE || 2 || 3
When numeric values (like "2" or "3") occur in a logical
expression, they are coerced to a logical TRUE (with the
one exception that a numerical value of "0" is coerced
to FALSE). Hence, whatever the outcome of "t==1" the result
will be either
TRUE || TRUE || TRUE
or
FALSE || TRUE || TRUE
which is always TRUE. This explains your results. Bill and Phil
have given you an alternative which works: "t %in% (1:3)".
A way of writing it (though longer) which is closer to your
original "wording" could be
if( (t==1) || (t==2) || (t==3) , 1, 0)
which really spells out how to force the parsing.
Hoping this helps,
Ted.
On 17-Mar-11 03:54:32, Bill.Venables at csiro.au wrote:
> It doesn't work (in R) because it is not written in R. It's written in
> some other language that looks a bit like R.
>
>> t <- 3
>> z <- t %in% 1:3
>> z
> [1] TRUE
>> t <- 4
>> z <- t %in% 1:3
>> z
> [1] FALSE
>
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of eric
> Sent: Thursday, 17 March 2011 1:26 PM
> To: r-help at r-project.org
> Subject: [R] Why doesn't this work ?
>
> Why doesn't this work and is there a better way ?
>
> z <-ifelse(t==1 || 2 || 3, 1,0)
> t <-3
> z
> [1] 1
> t <-4
> z
> [1] 1
>
> trying to say ...if t == 1 or if t== 2 or if t ==3 then true, otherwise
> false
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Why-doesn-t-this-work-tp3383656p3383656.ht
> ml
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
> ______________________________________________
> 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.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at wlandres.net>
Fax-to-email: +44 (0)870 094 0861
Date: 17-Mar-11 Time: 10:13:53
------------------------------ XFMail ------------------------------
More information about the R-help
mailing list