Open In App

R-Keywords

Last Updated : 05 Jun, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

R keywords are reserved words that have special meaning in the language. They help control program flow, define functions, and represent special values. We can check for which words are keywords by using the help(reserved) or ?reserved function.

R
help(reserved) # or "?reserved"

Output:

key
Reserved Key Words

Some Key Words in R Language

Here are some important R keywords and their brief explanations:

1. if

if is used for decision-making to execute code only if a condition is true.

R
a <- 5  
if (a > 0) {  
  print("Positive Number")  
}

Output:

[1] "Positive Number"

2. else

else executes code when the if condition is false.

R
x <- 5  
if (x > 10) {  
  print("Greater than 10")  
} else {  
  print("10 or less")  
}

Output:

[1] "10 or less"

3. while

while is a loop which runs a block repeatedly while a condition remains true.

R
val <- 1  
while (val <= 5) {  
  print(val)  
  val <- val + 1  
}

Output:

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

4. repeat

repeat runs a block indefinitely until explicitly stopped with break.

R
val <- 1  
repeat {  
  print(val)  
  val <- val + 1  
  if (val > 5) break  
}

Output:

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

5. for

for loop iterates over a sequence, running code for each element.

R
for (val in 1:5) {  
  print(val)  
}

Output:

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

6. function

function defines reusable blocks of code.

R
evenOdd <- function(x) {  
  if (x %% 2 == 0) return("even") else return("odd")  
}  
print(evenOdd(4))  
print(evenOdd(3))

Output:

[1] "even"
[1] "odd"

7. next

next skips the current iteration in a loop and continues with the next.

R
for (i in 6:11) {  
  if (i == 8) next  
  print(i)  
}

Output:

[1] 6
[1] 7
[1] 9
[1] 10
[1] 11

8. break

break stops the execution of a loop immediately.

R
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

9. TRUE / FALSE

Boolean constants representing logical true and false values.

R
x <- 4  
y <- 3  
print(x > y)  # TRUE  
print(x < y)  # FALSE

Output:

[1] TRUE
[1] FALSE

10. NULL

Represents an empty or undefined object.

R
v <- NULL  
print(v)  # NULL

Output:

NULL

11. Inf and NaN

Inf and -Inf represent positive and negative infinity. NaN means “Not a Number” and occurs in undefined numerical operations.

R
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

12. NA

Represents missing or unavailable data.

R
x <- c(1, NA, 2, 3)  
print(is.na(x)) 

Output:

[1] FALSE TRUE FALSE FALSE


Next Article
Article Tags :

Similar Reads