[R] simplify if statement in R ?

John Kane jrkrideau at yahoo.ca
Tue Jul 20 20:52:02 CEST 2010


Will something like the %in% statements below help?

x1 <- 1
x2 <- 5

y1 <- c(2,3)
y2 <- c(4,5)
y3 <- c(7,8)

x1 %in% y1 ||x2 %in%  y2
x1 %in% y1 ||x2 %in%  y3



--- On Tue, 7/20/10, Jim Maas <jimmaasuk at gmail.com> wrote:

> From: Jim Maas <jimmaasuk at gmail.com>
> Subject: [R] simplify if statement in R ?
> To: r-help at r-project.org
> Received: Tuesday, July 20, 2010, 2:11 PM
> 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?
> 
> Thanks
> 
> J
> 
> ----
> Jim Maas
> 
> ______________________________________________
> 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.
> 





More information about the R-help mailing list