R Tutorial

Fundamentals of R

Variables

Input and Output

Decision Making

Control Flow

Functions

Strings

Vectors

Lists

Arrays

Matrices

Factors

DataFrames

Object Oriented Programming

Error Handling

File Handling

Packages in R

Data Interfaces

Data Visualization

Statistics

Machine Learning with R

Keywords in R

Keywords in a programming language refer to reserved words that have a special meaning and functionality. In R, these keywords cannot be used as variable names or function names (unless you intentionally override them, which is not recommended).

In this tutorial, we will go through some of the fundamental keywords in R:

1. Control Structures

a. if, else

Used for conditional execution.

if (condition) {
    # code to execute if condition is TRUE
} else {
    # code to execute if condition is FALSE
}

b. repeat

Used to create an infinite loop, often paired with a break condition.

repeat {
  # code
  if (condition) break
}

c. while

Loop structure that continues execution as long as the condition is TRUE.

while (condition) {
  # code
}

d. for

Loop structure that iterates over a sequence.

for (variable in sequence) {
  # code
}

e. break

Used to exit a loop immediately.

f. next

Used to skip the current iteration of a loop.

2. Function Definition

a. function

Used to define a new function.

my_function <- function(arg1, arg2) {
  # code
}

3. Logical Keywords

a. TRUE, FALSE

Logical constants representing truth and falsehood.

b. NA

Represents missing or undefined data. It's logical by default, but there are other types like NA_integer_, NA_real_, etc.

c. NULL

Represents the absence of a value or a non-existent argument.

d. Inf, -Inf

Positive and negative infinity.

e. NaN

Stands for "not a number". It's the result of an undefined mathematical operation.

4. Basic Data Types

a. numeric

b. character

c. integer

d. logical

e. complex

5. Other Keywords

a. in

Used in the for loop to iterate over a sequence.

b. ...

Ellipsis, used in a function definition to indicate a variable number of arguments.

Conclusion

These keywords are essential building blocks in R programming. They are reserved for specific tasks, so avoid using them as names for your variables, functions, or datasets. Understanding and using them correctly will improve the clarity and efficiency of your R code.

  1. Reserved words in R programming: Reserved words in R are words that have special meaning and cannot be used as identifiers (variable or function names).

  2. R language keyword reference: R has several keywords that serve specific purposes. Some common ones include if, else, while, function, for, return, etc.

  3. Commonly used keywords in R:

    # Examples of commonly used keywords
    if (condition) {
      # code block
    } else {
      # code block
    }
    
    for (i in 1:5) {
      # code block
    }
    
    while (condition) {
      # code block
    }
    
  4. Keywords for control flow in R:

    # Control flow keywords
    if (condition) {
      # code block
    } else if (another_condition) {
      # code block
    } else {
      # code block
    }
    
    for (i in 1:5) {
      # code block
      if (i == 3) {
        break  # Exit the loop
      }
    }
    
    while (condition) {
      # code block
      if (some_condition) {
        next  # Skip to the next iteration
      }
    }
    
  5. Using keywords in R functions and expressions:

    # Example of using keywords in a function
    my_function <- function(x) {
      if (x > 0) {
        return("Positive")
      } else if (x < 0) {
        return("Negative")
      } else {
        return("Zero")
      }
    }
    
    # Example of using keywords in expressions
    result <- ifelse(x > 0, "Positive", "Non-Positive")