Skip to content
Prev 277520 / 398506 Next

if/else scope

On 11/15/2011 06:46 PM, Kevin Burton wrote:
x <- 1:2

#this works

if (x[1] > 0)
    { if (x[2] > 0)   print("1 & 2 > 0")  else  print("1 > 0")  } else
       { if (x[2] > 0) print("2 > 0")  else print("NONE > 0") }

#this works

if (x[1] > 0)
    { if (x[2] > 0)   print("1 & 2 > 0")
     else  print("1 > 0")  }         else
  { if (x[2] > 0) print("2 > 0")
     else print("NONE > 0") }

#this doesn't

if (x[1] > 0)
    { if (x[2] > 0)   print("1 & 2 > 0")
     else  print("1 > 0")  }
else  { if (x[2] > 0) print("2 > 0")  else print("NONE > 0") }


Conclusion: if-else in R is not quite free-form.