Vectors and lists are fundamental data structures in R. They help you organize, store, and manipulate data efficiently. Understanding how to use vectors and lists is essential for data analysis and statistical computing.
1. Vectors in R
A vector is a sequence of elements of the same data type (numeric, character, or logical). Vectors are one-dimensional and are widely used for storing data.
Creating Vectors
You can create a vector using the c() function:
numbers <- c(1, 2, 3, 4, 5) # Numeric vector
names <- c("Alice", "Bob", "Charlie") # Character vector
logical_values <- c(TRUE, FALSE, TRUE) # Logical vector
Accessing Vector Elements
Use square brackets [] to access elements:
numbers[1] # Returns 1 (first element)
names[2] # Returns "Bob" (second element)
Vector Operations
R allows arithmetic operations on vectors directly:
a <- c(1, 2, 3)
b <- c(4, 5, 6)
a + b # Returns c(5, 7, 9)
a * b # Returns c(4, 10, 18)
You can also use functions like sum(), mean(), length():
sum(a) # 6
mean(b) # 5
length(a) # 3
2. Lists in R
A list is a more flexible data structure than a vector. Lists can contain elements of different data types, including numbers, strings, vectors, and even other lists.
Creating Lists
my_list <- list(
name = "Alice",
age = 25,
scores = c(90, 85, 88),
passed = TRUE
)
Accessing List Elements
Use $ or double square brackets [[]] to access elements:
my_list$name # Returns "Alice"
my_list[["age"]] # Returns 25
my_list$scores[2] # Returns 85
Modifying Lists
You can add or update elements easily:
my_list$city <- "New York" # Adds a new element
my_list$age <- 26 # Updates age
Differences Between Vectors and Lists
- Vectors can only contain elements of the same type; lists can hold mixed types.
- Vectors are one-dimensional; lists can contain other lists or complex structures.
- Lists are ideal for storing structured data, while vectors are best for numerical or homogeneous data.
Conclusion
Vectors and lists are core data structures in R. Vectors are simple, one-dimensional sequences of the same type, while lists are flexible containers for mixed data types. Mastering these will help you efficiently organize, analyze, and manipulate data in R.