In this guide we will discuss about Keywords in R Programming.
In programming, a keyword is a word which is reserved by a program because it has a special meaning. A keyword can be a command or a parameter. Like in C, C++, Java, there is also a set of keywords in R. A keyword can’t be used as a variable name. Keywords are also called as “reserved names.”
There are the following keywords as per ?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_ |
1) if
The if statement consists of a Boolean expression which is followed by one or more statements. In R, if statement is the simplest conditional statement which is used to decide whether a block of the statement will be executed or not.
Example:
a<-11 if(a<15) + print("I am lesser than 15")
Output:
2) else
The R else statement is associated with if statement. When the if statement’s condition is false only then else block will be executed. Let see an example to make it clear:
Example:
a<-22 if(a<20){ cat("I am lesser than 20") }else{ cat("I am larger than 20") }
Output:
3) repeat
The repeat keyword is used to iterate over a block of code multiple numbers of times. In R, repeat is a loop, and in this loop statement, there is no condition to exit from the loop. For exiting the loop, we will use the break statement.
Example:
x <- 1 repeat { cat(x) x = x+1 if (x == 6){ break } }
Output:
4) while
A while keyword is used as a loop. The while loop is executed until the given condition is true. This is also used to make an infinite loop.
Example
a <- 20 while(a!=0){ cat(a) a = a-2 }
Output:
5) function
A function is an object in R programming. The keyword function is used to create a user-define function in R. R has some pre-defined functions also, such as seq, mean, and sum.
Example:
new.function<- function(n) { for(i in 1:n) { a <- i^2 print(a) } } new.function(6)
Output
6) for
The for is a keyword which is used for looping or iterating over a sequence (dictionary, string, list, set or tuple).
We can execute a set of a statement once for each item in the iterator (list, set, tuple, etc.) with the help of for loop.
Example:
v <- LETTERS[1:4] for ( i in v) { print(i) }
Output
7) next
The next keyword skips the current iteration of a loop without terminating it. When R parser found next, it skips further evaluation and starts the new iteration of the loop.
Example:
v <- LETTERS[1:6] for ( i in v) { if (i == "D") { next } print(i) }
Output
8) break
The break keyword is used to terminate the loop if the condition is true. The control of the program firstly passes to the outer statement then passes to the body of the break statement.
Example:
n<-1 while(n<10){ if(n==3) break n=n+1 cat(n,"\n") } cat("End of the program")
Output:
9) 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.
10) 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:
as.null(list(a = 1, b = "c"))
Output:
11) Inf and NaN
The is.finite and is.infinite function returns a vector of the same length indicating which elements are finite or infinite.
Inf and -Inf are positive and negative infinity. NaN stands for ‘Not a Number.’ NaN applies on numeric values and real and imaginary parts of complex values, but it will not apply to the values of integer vectors.
Usage
is.finite(x) is.infinite(x) is.nan(x) Inf NaN
12) NA
NA is a logical constant of length 1 that contains a missing value indicator. It can be coerced to any other vector type except raw. There are other types of constant also, such as NA_Integer_, NA_real_, NA_complex_, and NA_character. These constants are of the other atomic vector type which supports missing values.
Usage
NA is.na(x) anyNA(x, recursive = FALSE) ## S3 method for class 'data.frame' is.na(x) is.na(x) <- value
Next Topic : Click Here
Pingback: Variables in R Programming | Adglob Infosystem Pvt Ltd
A round of applause for your article.Thanks Again. Cool.