R Keywords

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

  1. if: Used to start a conditional statement.
  2. else: Used to specify an alternative block of code if the condition in the if statement is not met.
  3. for: Used to create a loop that iterates over a sequence.
  4. while: Used to create a loop that continues as long as a condition is TRUE.
  5. repeat: Used to create an infinite loop, typically controlled by a break statement inside the loop.
  6. break: Used to exit a loop.
  7. next: Used to skip the current iteration of a loop and proceed to the next iteration.

Function Keywords

  1. function: Used to define a function.
  2. return: Used to return a value from a function.

Data Structure Keywords

  1. NULL: Represents a null object.
  2. TRUE: Represents the logical value TRUE.
  3. FALSE: Represents the logical value FALSE.
  4. NA: Represents a missing value.
  5. Inf: Represents positive infinity.
  6. NaN: Represents ‘Not a Number’.

Other Keywords

  1. NA_integer_: Represents a missing integer value.
  2. NA_real_: Represents a missing real number.
  3. NA_complex_: Represents a missing complex number.
  4. NA_character_: Represents a missing character value.
  5. Inf: Represents positive infinity.
  6. NaN: Represents ‘Not a Number’.
  7. TRUE: Represents the logical value TRUE.
  8. FALSE: Represents the logical value FALSE.
  9. NULL: Represents a null object.
  10. NA: Represents a missing value.
  11. Inf: Represents positive infinity.
  12. NaN: Represents ‘Not a Number’.
  13. : 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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top