There is a list of words already defined in the R library. These reserved words are called Keywords in R programming. Every keyword has a specific meaning and is defined to serve a specific purpose. The R keywords are used in R programs to perform a specific operation during execution. A Keyword in C is prohibited to be used as a variable name.
R has the below-defined keywords according to the ?reserved or help(reserved) command:
- if
- else
- repeat
- while
- function
- for
- next
- break
- TRUE
- FALSE
- NULL
- Inf
- NaN
- NA
- NA_integer_
- NA_real_
- NA_complex_
- NA_character_
If statement:
The if statement is a conditional statement in R. It is the simplest form of conditional statement, where a block of statement is executed only if the mentioned condition is True.
Example:
age>20
if(age>40)
+ print(“My age is more than 40.”)
Else statement:
The else statement is a conditional statement associated with the if statement. The statements in the else block are executed only if the conditions mentioned in the if statement is False. Otherwise, the block of statement associated with the if statement will be executed.
Example:
age>20
if(age>40){
cat(“My age is more than 40.”)
}else{
cat(“My age is more than 20 and less than or equal to 40.”)
}
Repeat statement:
To execute a block of code more than once in a continuous loop, the repeat keyword is utilised in R. However, the repeat statement presents no condition to exit from the loop, and this is where the need for the break statement arises.
Example:
var ← 2
repeat {
cat(var)
var = var+2
if (var == 10){
break
}
}
While statement:
To execute a block of code more than once in a continuous loop, the while keyword is utilised in R, similar to the repeat statement. However, unlike the repeat statement, the while statement presents a condition to exit from the loop. An infinite loop can also be executed using the while loop.
Example:
var ← 2
while(var!=10){
cat(var)
var = var+2
}
Function keyword:
Some functions are already defined in the R library. These functions are also known as pre-defined functions. Example: seq, sum, mean, etc.
However, for efficient programming, we might come across a requirement for a user-defined function. Creating a user-defined function is possible in R with the function keyword.
Example:
new.function← function(n) {
for(i in 1:n) {
var ← i*44
print(var)
}
}
new.function(9)
For Loop:
For is a keyword reserved for a loop statement. The for loop executes a block of statements continuously as long as the condition of the loop is true. The loop stops only when the condition is false. Thus, the block of codes executes only a specified number of times over a sequence (dictionary, string, list, set or tuple).
Example:
var ← LETTERS[1:6]
for ( i in var) {
print(i)
}
Next keyword:
The next keyword is used inside a loop to skip the current iteration of a loop. It does not break the loop or stop it, it just starts a new iteration of the loop.
Example:
var ← LETTERS[1:9]
for ( i in var) {
if (i == “F”) {
next
}
print(i)
}
Break statement:
The break keyword is used inside a loop to stop further iteration of a loop. Unlike the next statement, it completely terminates the execution of the loop and brings the code out of the loop.
Example:
var ← 2
while(var<20){
if(var==18)
break
var=var+2
cat(var,”\n”)
}
cat(“Execution Completed.”)
TRUE keyword:
The TRUE keyword represents a Boolean true and is thus a reserved word in R. If the output after the execution of a block of statement is TRUE, it means the given statement is true.
FALSE keyword:
The FALSE keyword represents a Boolean false and is thus a reserved word in R. If the output after the execution of a block of statement is FALSE, it means the given statement is false.
NULL keyword:
For a statement which is neither TRUE nor FALSE, it is expressed logically as NULL in R programming. The NULL keyword simply depicts the null object, i.e., missing and undefined values.
Example:
as.null(list(var_1 = 22, var_2 = “R”))
Inf keyword:
In R programming, the output of the is.finite and is.infinite function is a vector of the same length. They, thus, signify which elements are finite or infinite. The Inf keyword represents positive infinity and the -Inf keyword represents negative infinity.
Uses:
is.finite(x)
is.infinite(x)
Inf
Nan keyword:
In R, the NaN keyword is reserved for expressing ‘Not a Number.’ It does not apply to the values of integer vectors but only to the numeric values and real and imaginary parts of complex values.
Uses:
is.nan(x)
NaN
NA keyword:
In R programming, the NA keyword represents a logical constant of length 1. NA includes a missing value indicator. It is applicable for all the vector types except raw.
Uses:
NA
is.na(x)
anyNA(x, recursive = FALSE)
is.na(x) <- value
Some constants of other atomic vector types that support missing values are:
NA_Integer_
NA_real_
NA_complex_
NA_character