[R] simplify if statement in R ?
Duncan Murdoch
murdoch.duncan at gmail.com
Tue Jul 20 20:30:34 CEST 2010
On 20/07/2010 2:11 PM, Jim Maas wrote:
> I found a form of the if statement that works but it is very long. I'm
> attempting to check the value of of two different variables to see if
> they evaluate to either of two different values, which will result in a
> division by 0 in the final equation. This first if statement works for me
> _______
>
> if ((x0.trial01 == 0.0)||(x0.trial01 == npt01)||(x1.trial01 ==
> 0.0)||(x1.trial01 == npt01))
> {
> x0.trial01.in <- x0.trial01 + 0.5
> x1.trial01.in <- x1.trial01 + 0.5
> npt01.in <- npt01 + 1.0
> }
> else
> {
> x0.trial01.in <- x0.trial01
> x1.trial01.in <- x1.trial01
> npt01.in <- npt01
> }
> __________
>
> I've tried
> if ( ( x0.trial01 || x1.trial01) == ( 0.0 || npt01) )
> ......
>
>
> but it doesn't seem to work correctly. Is there a simpler way to check
> two variables for two different valuables?
I would probably use your original one, formatted to make the repetition
clear, e.g.
if ( (x0.trial01 == 0.0)
||(x0.trial01 == npt01)
||(x1.trial01 == 0.0)
||(x1.trial01 == npt01))
but if you really want less repetition, you could go with this:
if ( length( intersect( c(x0.trial01, x1.trial01), c(0.0, npt01) ) ) > 0 )
Doesn't look clearer to me, but it fits on one line....
Duncan Murdoch
More information about the R-help
mailing list