R – Keywords
Last Updated :
03 Apr, 2023
R is an open-source programming language that is widely used as a statistical software and data analysis tool. R generally comes with the Command-line interface. R is available across widely used platforms like Windows, Linux, and macOS. Also, the R programming language is the latest cutting-edge tool.
Keywords are specific reserved words in R, each of which has a specific feature associated with it. Almost all of the words which help one to use the functionality of the R language are included in the list of keywords. So one can imagine that the list of keywords is not going to be a small one! In R, one can view these keywords by using either help(reserved)
or ?reserved
. Here is the list of keywords in R:
function |
in |
next |
break |
TRUE |
NA_integer |
NA_real |
NA_complex_ |
NA_character_ |
… |
Following are some most important keywords along with their examples:
- if: If statement is one of the Decision-making statements in the R programming language. It is one of the easiest decision-making statements. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.
Example:
a <- 5
if ( a > 0 )
{
print ( "Positive Number" )
}
|
Output:
Positive Number
- else: It is similar to if statement but when the test expression in if condition fails, then statements in else condition are executed.
Example:
x <- 5
if (x > 10)
{
print ( paste (x, "is greater than 10" ))
}
else
{
print ( paste (x, "is less than 10" ))
}
|
Output:
[1] "5 is less than 10"
- while: It is a type of control statement which will run a statement or a set of statements repeatedly unless the given condition becomes false. It is also an entry controlled loop, in this loop the test condition is tested first, then the body of the loop is executed, the loop body would not be executed if the test condition is false.
Example:
val = 1
while (val <= 5 )
{
print (val)
val = val + 1
}
|
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
- repeat: It is a simple loop that will run the same statement or a group of statements repeatedly until the stop condition has been encountered. Repeat loop does not have any condition to terminate the loop, a programmer must specifically place a condition within the loop’s body and use the declaration of a break statement to terminate this loop. If no condition is present in the body of the repeat loop then it will iterate infinitely.
Example:
val = 1
repeat
{
print (val)
val = val + 1
if (val > 5)
{
break
}
}
|
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
- for: It is a type of control statement that enables one to easily construct a loop that has to run statements or a set of statements multiple times. For loop is commonly used to iterate over items of a sequence. It is an entry controlled loop, in this loop the test condition is tested first, then the body of the loop is executed, the loop body would not be executed if the test condition is false.
Example:
for (val in 1:5)
{
print (val)
}
|
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
- function: Functions are useful when you want to perform a certain task multiple number of times. In R functions are created using function keyword.
Example:
evenOdd = function (x){
if (x %% 2 == 0)
return ( "even" )
else
return ( "odd" )
}
print ( evenOdd (4))
print ( evenOdd (3))
|
Output:
[1] "even"
[1] "odd"
- next:Next statement in R is used to skip any remaining statements in the loop and continue the execution of the program. In other words, it is a statement that skips the current iteration without loop termination.
Example:
val <- 6:11
for (i in val)
{
if (i == 8)
{
next
}
print (i)
}
|
Output:
[1] 6
[1] 7
[1] 9
[1] 10
[1] 11
- break: The break keyword is a jump statement that is used to terminate the loop at a particular iteration.
Example:
a<-1
while (a < 10)
{
print (a)
if (a == 5)
break
a = a + 1
}
|
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
- TRUE/FALSE: The TRUE and FALSE keywords are used to represent a Boolean true and Boolean false. If the given statement is true, then the interpreter returns true else the interpreter returns false.
Example:
x = 4
y = 3
z = x > y
p = x < y
print (z)
print (p)
|
Output:
[1] TRUE
[1] FALSE
- NULL: In R, NULL represents the null object. NULL is used to represent missing and undefined values. NULL is the logical representation of a statement which is neither TRUE nor FALSE.
Example:
v = as.null ( c (1, 2, 3, 4))
print (v)
|
Output:
NULL
- Inf and NaN: In R
is.finite
and is.infinite
return a vector of the same length as x, where x is an R object to be tested. This indicating which elements are finite (not infinite and not missing) or infinite. Inf and -Inf keyword mean positive and negative infinity whereas NaN keyword means ‘Not a Number’.
x = c ( Inf , 2, 3)
print ( is.finite (x))
y = c (1, NaN , 3)
print ( is.nan (y))
|
Output:
[1] FALSE TRUE TRUE
[1] FALSE TRUE FALSE
- NA: NA stands for “Not Available” and is used to represent missing values. There are also constants NA_integer_, NA_real_, NA_complex_ and NA_character_ of the other atomic vector types which support missing values and all of these are reserved words in the R language.
x = c (1, NA , 2, 3)
print ( is.na (x))
|
Output:
[1] FALSE TRUE FALSE FALSE
Share your thoughts in the comments
Please Login to comment...