O'Reilly logo

R Data Visualization Cookbook by Atmajitsinh Gohil

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Writing if else statements in R

We often use if statements in MS Excel, but we can also write a small code to perform simple tasks in R.

How to do it…

The logic for if else statements is very simple and is as follows:

if(x>3){
  print("greater value")
}else {
  print("lesser value")
}

We can copy and paste the preceding statement in the R console or write a function that makes use of the if else logic.

How it works…

The logic behind if else statements is very simple. The following lines clearly state the logic:

if(condition){
#perform some action
}else {
  #perform some other action
}

The preceding code will check whether x is greater than or less than 3, and simply print it. In order to get the value, we type the following in the R command window:

x = 2

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required