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
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
Hi Jim, Without a reproducible example I can't test my solution against yours, but does this do what you are looking for? #Some Sample data x0.trial01 <- 1 x1.trial01 <- 2 npt01 <- 2 if(any(c(x0.trial01, x1.trial01) %in% c(0, 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 } It simply tests whether any variables in the first vector (I combined using c() ) match any variables in the second vector. HTH, Josh On Tue, Jul 20, 2010 at 11:11 AM, Jim Maas <jimmaasuk at gmail.com> 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? > > 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
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. >