Introduction
In this chapter, you will learn about various data structures in R. Understanding these data structures is crucial for effectively managing and manipulating data in your R programs. The main data structures in R include vectors, lists, matrices, arrays, data frames, and factors.
Vectors
Vectors are the most basic data structures in R. They are one-dimensional arrays that can hold numeric, character, or logical data.
Creating Vectors
Example:
# Numeric vector
num_vec <- c(1, 2, 3, 4, 5)
print(num_vec)
# Character vector
char_vec <- c("a", "b", "c", "d", "e")
print(char_vec)
# Logical vector
log_vec <- c(TRUE, FALSE, TRUE, TRUE, FALSE)
print(log_vec)
Accessing Elements
Example:
# Accessing elements
print(num_vec[1]) # Output: 1
print(char_vec[2]) # Output: "b"
print(log_vec[3]) # Output: TRUE
Vector Operations
Example:
# Vector operations
vec1 <- c(1, 2, 3)
vec2 <- c(4, 5, 6)
# Addition
print(vec1 + vec2) # Output: 5 7 9
# Subtraction
print(vec1 - vec2) # Output: -3 -3 -3
# Multiplication
print(vec1 * vec2) # Output: 4 10 18
# Division
print(vec1 / vec2) # Output: 0.25 0.4 0.5
Lists
Lists are versatile data structures that can hold elements of different types, including other lists.
Creating Lists
Example:
# Creating a list
my_list <- list(num_vec, char_vec, log_vec)
print(my_list)
Accessing Elements
Example:
# Accessing elements
print(my_list[[1]]) # Access first element (num_vec)
print(my_list[[2]][3]) # Access third element of char_vec
Matrices
Matrices are two-dimensional arrays that can hold numeric, character, or logical data. All elements in a matrix must be of the same type.
Creating Matrices
Example:
# Creating a matrix
mat <- matrix(1:9, nrow = 3, ncol = 3)
print(mat)
Accessing Elements
Example:
# Accessing elements
print(mat[1, 2]) # Access element at first row, second column
print(mat[, 3]) # Access all elements of the third column
print(mat[2, ]) # Access all elements of the second row
Arrays
Arrays are multi-dimensional generalizations of matrices.
Creating Arrays
Example:
# Creating an array
arr <- array(1:12, dim = c(3, 2, 2))
print(arr)
Accessing Elements
Example:
# Accessing elements
print(arr[1, , ]) # Access all elements of the first row across all dimensions
print(arr[, 2, 1]) # Access all elements of the second column in the first matrix
Data Frames
Data frames are two-dimensional data structures that can hold elements of different types (numeric, character, logical) in each column.
Creating Data Frames
Example:
# Creating a data frame
df <- data.frame(Name = c("Alice", "Bob", "Charlie"),
Age = c(25, 30, 35),
Gender = c("F", "M", "M"))
print(df)
Accessing Elements
Example:
# Accessing elements
print(df$Name) # Access the 'Name' column
print(df[1, ]) # Access the first row
print(df[, 2]) # Access the second column
print(df[2, 3]) # Access element at second row, third column
Factors
Factors are used to represent categorical data. They can be ordered or unordered.
Creating Factors
Example:
# Creating a factor
factor_data <- factor(c("low", "medium", "high", "medium", "low"))
print(factor_data)
print(levels(factor_data)) # Output: "high" "low" "medium"
Changing Factor Levels
Example:
# Changing factor levels
levels(factor_data) <- c("low", "medium", "high")
print(factor_data)
Example Program Using Different Data Structures
Here is an example program that demonstrates the use of various data structures in R:
# Example program using different data structures
# Vectors
num_vec <- c(1, 2, 3, 4, 5)
char_vec <- c("a", "b", "c", "d", "e")
log_vec <- c(TRUE, FALSE, TRUE, TRUE, FALSE)
# Lists
my_list <- list(num_vec, char_vec, log_vec)
# Matrices
mat <- matrix(1:9, nrow = 3, ncol = 3)
# Arrays
arr <- array(1:12, dim = c(3, 2, 2))
# Data Frames
df <- data.frame(Name = c("Alice", "Bob", "Charlie"),
Age = c(25, 30, 35),
Gender = c("F", "M", "M"))
# Factors
factor_data <- factor(c("low", "medium", "high", "medium", "low"))
# Print data structures
print("Numeric Vector:")
print(num_vec)
print("Character Vector:")
print(char_vec)
print("Logical Vector:")
print(log_vec)
print("List:")
print(my_list)
print("Matrix:")
print(mat)
print("Array:")
print(arr)
print("Data Frame:")
print(df)
print("Factor:")
print(factor_data)
Conclusion
In this chapter, you learned about various data structures in R, including vectors, lists, matrices, arrays, data frames, and factors. These data structures are fundamental for managing and manipulating data in R. By mastering these data structures, you can efficiently handle different types of data and perform complex data analysis tasks in your R programs.