Introduction
In this chapter, you will learn about keywords in R. Keywords are reserved words that have special meaning in the R programming language. They are used to define the structure and control the flow of R programs. Understanding these keywords is essential for writing correct and efficient R code.
List of R Keywords
Here is a list of some of the most commonly used keywords in R:
Control Flow Keywords
- if: Used to start a conditional statement.
- else: Used to specify an alternative block of code if the condition in the
if
statement is not met. - for: Used to create a loop that iterates over a sequence.
- while: Used to create a loop that continues as long as a condition is
TRUE
. - repeat: Used to create an infinite loop, typically controlled by a
break
statement inside the loop. - break: Used to exit a loop.
- next: Used to skip the current iteration of a loop and proceed to the next iteration.
Function Keywords
- function: Used to define a function.
- return: Used to return a value from a function.
Data Structure Keywords
- NULL: Represents a null object.
- TRUE: Represents the logical value
TRUE
. - FALSE: Represents the logical value
FALSE
. - NA: Represents a missing value.
- Inf: Represents positive infinity.
- NaN: Represents ‘Not a Number’.
Other Keywords
- NA_integer_: Represents a missing integer value.
- NA_real_: Represents a missing real number.
- NA_complex_: Represents a missing complex number.
- NA_character_: Represents a missing character value.
- Inf: Represents positive infinity.
- NaN: Represents ‘Not a Number’.
- TRUE: Represents the logical value
TRUE
. - FALSE: Represents the logical value
FALSE
. - NULL: Represents a null object.
- NA: Represents a missing value.
- Inf: Represents positive infinity.
- NaN: Represents ‘Not a Number’.
- …: Used to pass a variable number of arguments to a function.
Example Program Using Keywords
Here is an example program that demonstrates the use of various keywords in R:
# R Program to Demonstrate Keywords
# Define a function
myFunction <- function(x) {
# Check if x is greater than 10
if (x > 10) {
return("x is greater than 10")
} else {
return("x is 10 or less")
}
}
# Loop through a sequence of numbers
for (i in 1:15) {
# Use the function and print the result
result <- myFunction(i)
print(paste("i =", i, ":", result))
}
# While loop example
count <- 1
while (count <= 5) {
print(paste("Count:", count))
count <- count + 1
}
# Repeat loop example
count <- 1
repeat {
print(paste("Repeat count:", count))
count <- count + 1
if (count > 3) {
break
}
}
# Logical values
isTrue <- TRUE
isFalse <- FALSE
# Print logical values
print(paste("isTrue:", isTrue))
print(paste("isFalse:", isFalse))
# NA values
naVal <- NA
print(paste("NA value:", naVal))
# Infinity and NaN
infVal <- Inf
nanVal <- NaN
print(paste("Infinity value:", infVal))
print(paste("NaN value:", nanVal))
Conclusion
In this chapter, you learned about the various keywords in R, including those used for control flow, functions, data structures, and other special purposes. Keywords are reserved words that have specific meanings in the R programming language, and they are essential for defining the structure and behavior of your R programs. Understanding and using these keywords correctly will help you write more effective and efficient R code.