OneBite.Dev - Coding blog in a bite size

if else conditional statement in R

Code snippet on how to if else conditional statement in R

  if(x < 0) {
    result <- "Negative"
  } else if (x > 0) {
    result <- "Positive"
  } else {
    result <- "Zero"
  }

This code checks the value of x and assigns a different value to the result variable depending on the outcome. First, it checks if the value of x is less than 0. If so, the value of result is set to “Negative”. Otherwise, it continues and checks if the value of x is greater than 0. If true, the value of result is set to “Positive”. If neither of these conditions is true, the value of result is set to “Zero”.

r