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 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:
if
, else
Used for conditional execution.
if (condition) { # code to execute if condition is TRUE } else { # code to execute if condition is FALSE }
repeat
Used to create an infinite loop, often paired with a break condition.
repeat { # code if (condition) break }
while
Loop structure that continues execution as long as the condition is TRUE
.
while (condition) { # code }
for
Loop structure that iterates over a sequence.
for (variable in sequence) { # code }
break
Used to exit a loop immediately.
next
Used to skip the current iteration of a loop.
function
Used to define a new function.
my_function <- function(arg1, arg2) { # code }
TRUE
, FALSE
Logical constants representing truth and falsehood.
NA
Represents missing or undefined data. It's logical by default, but there are other types like NA_integer_
, NA_real_
, etc.
NULL
Represents the absence of a value or a non-existent argument.
Inf
, -Inf
Positive and negative infinity.
NaN
Stands for "not a number". It's the result of an undefined mathematical operation.
numeric
character
integer
logical
complex
in
Used in the for
loop to iterate over a sequence.
...
Ellipsis, used in a function definition to indicate a variable number of arguments.
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.
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).
R language keyword reference:
R has several keywords that serve specific purposes. Some common ones include if
, else
, while
, function
, for
, return
, etc.
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 }
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 } }
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")