Javatpoint Logo
Javatpoint Logo

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_
R Programming Keywords

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:

Output:

R Programming Keywords

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:

Output:

R Programming Keywords

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:

Output:

R Programming Keywords

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:

Output:

R Programming Keywords

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:

Output:

R Programming Keywords

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:

Output:

R Programming Keywords

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:

Output:

R Programming Keywords

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:

Output:

R Programming Keywords

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.

R Programming Keywords

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:

Output:

R Programming Keywords

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

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


Next TopicR Operators




Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA