Keywords in R

Keywords in R

  • R
  • 4 mins read

In this article, we will study the basic concepts and usage of keywords in R. 

Keywords in R are basically words with a specific purpose associated with them. They are the special words in R each of which serves a designated operation. These words are already designated by the compiler. The functionality of the R programs is ensured by the usage of these keywords. R contains a large set of keywords within its collection, which can be accessed using the following command : 

help(reserved) 

?reserved
ifelserepeat
whilefunctionfor
nextbreakTRUE
FALSENULLInf
NaNNANA_integer_
NA_real_NA_complex_NA_character_

Some of the keywords in R are int, float, and conditionals, such as if-else, etc. These keywords can’t be used as normal identifiers. In any programming language, the number of keywords is limited by nature, which forms the basic building blocks. 

Let us discuss the usage of some of these keywords in detail.

Boolean variables: TRUE and FALSE

Most of the statements in R have two logical values associated with them, either a TRUE or a FALSE value. The TRUE boolean is assigned to an expression only when its evaluation returns the result in accordance with the input data already available. In case the evaluation result and the input data are contradictory in nature, the result becomes equivalent to a boolean FALSE value. 

#declaring a variable with value= 4 
var1 = 4

#checking if the result is greater than 2 and assigning to res1
res1 = (var1 > 2)

#printing the value upon evaluation of res1
print("Is the value of var1 greater than 2")
print(res1)

#checking if the value of var1 is equal to 5 and assigning to res2
res2 = (var1==5)

#printing the value upon evaluation of res2
print("Is the value of var1 equal to 5")
print(res2)

The code produces the following output on the console

[1] "Is the value of var1 greater than 2"
[1] TRUE
[1] "Is the value of var1 equal to 5"
[1] FALSE

Conditionals: if and else

If and else are two separate keywords in R which are together used to enforce the solutions to problems where decision-making is involved. An if-else statement block in R is used to evaluate the test expression. If the test expression is TRUE, the code in the if block is executed, else the code in the ELSE block gets executed. One of the blocks is definitely executed, providing the statement has a true/false result. The else block is specified in the same block where the if condition ends. 

if(test-condition)
{
#code executed if test-condition is true
}else
{
# code executed if test-condition is false
}

The above code snippet can also be easily written in the form of an if-else block and see if the condition evaluates to true, and gets executed in which block. 

#declaring a variable with value= 4 
var1 = 4

#checking if var1 is equal to 4
if(var1 == 4){
  #execute this code if condition is satisifed
  print("TRUE : var1 is equal to 4")
}
else{
  #execute this code if condition is not satisifed
  print("FALSE : var1 is not equal to 4")
}

Since, the value of var1 is equivalent to 4, the code in the if block is executed. The code evaluates to the following output. 

[1] "TRUE : var1 is equal to 4"

NULL

A null value in R is used to represent any undefined or missing value. It is equal to declaring a null object, indicative of the fact that the value is unavailable. In other words, we can say that the logical value of any such statement assigned to a null value is neither TRUE nor FALSE. The value of any NULL variable can be checked by calculating its length. 

#declaring a variable with value= 4 
var1 <- NULL

#print the length of var1
len <- length(var1)

print("length of var1")
print(len)

The code produces 0 as the length of variable 
[1] "length of var1"
[1] 0

LOOPS: for and while

The loops are used to execute a set of statements until the given specified condition is met. This condition is known as the limiting condition. The loops majorly fall into the following categories, for and while.

The following code snippet illustrates the usage of the for loop.

#using for loop to print sequence from 1 to 5
for(i in 1:5){
  #printing the value of i
  print(i)
}

The output produced is as follows :

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

The functionality of more such keywords will be studied as we understand the concepts and syntax associated with them.

Read Also: