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
In R, a list is a versatile data structure that can contain multiple objects of different types. A common use case is a list of vectors. Here's a tutorial on how to work with a list of vectors in R:
Let's create three sample vectors and put them in a list:
vec1 <- c(1, 2, 3) vec2 <- c("apple", "banana", "cherry") vec3 <- c(TRUE, FALSE, TRUE) list_of_vecs <- list(vec1, vec2, vec3)
You can access vectors in the list by their index:
list_of_vecs[[2]] # This accesses vec2
You can assign names to the vectors in the list for easier access:
names(list_of_vecs) <- c("Numbers", "Fruits", "Booleans") list_of_vecs$Fruits # This accesses vec2
Use the lapply()
function to apply a function to each element of the list:
# Get the length of each vector in the list lengths <- lapply(list_of_vecs, length)
You can modify vectors in the list like this:
list_of_vecs$Numbers <- c(list_of_vecs$Numbers, 4) # Adds the number 4 to vec1
You can easily add a new vector to the list:
vec4 <- c(4.5, 5.5, 6.5) list_of_vecs$Decimals <- vec4
You can set the vector's value to NULL
to remove it:
list_of_vecs$Numbers <- NULL
You can use loops to iterate over each vector in the list:
for(vec_name in names(list_of_vecs)) { print(paste("Vector:", vec_name)) print(list_of_vecs[[vec_name]]) }
If all vectors in the list have the same length, you can convert the list to a dataframe:
df <- as.data.frame(list_of_vecs)
Lists in R allow for structured data storage and operations on collections of vectors. Whether you're organizing related vectors, performing batch operations, or preparing data for analysis, lists offer a flexible and efficient data structure to work with in R.
Creating a list of vectors in R:
Overview: Learn how to create a list containing multiple vectors.
Code:
# Creating a list of vectors vector1 <- c(1, 2, 3) vector2 <- c(4, 5, 6) vector_list <- list(vector1, vector2) # Display the list of vectors print("List of Vectors:") print(vector_list)
Manipulating lists of vectors in R:
Overview: Explore common operations on lists of vectors, such as subsetting and modification.
Code:
# Accessing and modifying elements in the list vector_list[[1]][2] <- 20 # Display the modified list of vectors print("Modified List of Vectors:") print(vector_list)
R combine vectors into a list:
Overview: Learn different methods to combine existing vectors into a list.
Code:
# Combining vectors into a list vector3 <- c(7, 8, 9) combined_vector_list <- list(vector1, vector2, vector3) # Display the combined list of vectors print("Combined List of Vectors:") print(combined_vector_list)
Accessing elements in a list of vectors in R:
Overview: Understand how to access specific vectors within a list.
Code:
# Accessing specific vector in the list vector_subset <- combined_vector_list[[2]] # Display the accessed vector print("Accessed Vector:") print(vector_subset)
Appending vectors to a list in R:
Overview: Learn how to add new vectors to an existing list.
Code:
# Appending a new vector to the list vector4 <- c(10, 11, 12) combined_vector_list <- c(combined_vector_list, list(vector4)) # Display the updated list of vectors print("Updated List of Vectors:") print(combined_vector_list)
R lapply with list of vectors example:
Overview: Use lapply
to apply a function to each vector in a list.
Code:
# Applying a function to each vector in the list modified_vector_list <- lapply(combined_vector_list, function(vec) { vec * 2 }) # Display the modified list of vectors print("Modified List of Vectors:") print(modified_vector_list)
Converting list of vectors to a single vector in R:
Overview: Combine all vectors in a list into a single vector.
Code:
# Converting list of vectors to a single vector combined_vector <- unlist(combined_vector_list) # Display the combined vector print("Combined Vector:") print(combined_vector)
R split vector into a list of vectors:
Overview: Learn how to split a vector into a list of vectors based on a specific criterion.
Code:
# Splitting a vector into a list of vectors split_vector_list <- split(combined_vector, rep(1:2, each = 3)) # Display the split list of vectors print("Split List of Vectors:") print(split_vector_list)