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 two-dimensional list doesn't have a direct equivalent like in some other languages. However, you can think of a list of lists as a two-dimensional list. Each element of the main list can itself be a list, allowing for a hierarchical or nested structure.
Here's a tutorial on how you can create, manipulate, and use a two-dimensional list in R.
# Creating a list of lists list2D <- list( list(name="John", age=25, scores=c(85, 90, 88)), list(name="Jane", age=22, scores=c(80, 78, 89)), list(name="Doe", age=24, scores=c(90, 92, 85)) ) print(list2D)
# Accessing the first element (which is itself a list) print(list2D[[1]]) # Accessing the 'name' of the first list print(list2D[[1]]$name) # Accessing the first score of the first list print(list2D[[1]]$scores[1])
# Modify the name of the first list list2D[[1]]$name <- "Mike" print(list2D[[1]]$name)
You can grow the main list by adding more lists to it.
new_entry <- list(name="Anna", age=23, scores=c(75, 80, 82)) list2D <- c(list2D, list(new_entry)) print(list2D)
# Remove the third entry list2D <- list2D[-3] print(list2D)
Use a for
loop to iterate over each list inside the main list.
for (entry in list2D) { cat("Name:", entry$name, " Age:", entry$age, "\n") }
sapply
or lapply
:These functions can be handy when you want to extract or compute something from each sub-list.
# Extract all names names <- sapply(list2D, function(x) x$name) print(names) # Compute the average score for each person avg_scores <- sapply(list2D, function(x) mean(x$scores)) print(avg_scores)
While R doesn't have a native "two-dimensional list" in the same way that some languages might have 2D arrays or lists, the list of lists approach in R provides a flexible way to handle nested structures. The combination of lists and built-in functions in R allows for efficient management and manipulation of hierarchical data structures.
Creating Two-Dimensional List in R:
# Example: Creating a two-dimensional list two_dim_list <- list(list(1, 2, 3), list(4, 5, 6), list(7, 8, 9))
Nested Lists in R:
# Example: Nested lists nested_list <- list(list("a", "b", "c"), list(1, 2, 3))
List of Lists in R:
# Example: List of lists list_of_lists <- list(list(10, 20, 30), list(40, 50, 60))
Working with Lists as Matrices in R:
# Example: Using lists as matrices list_matrix <- list(c(1, 2, 3), c(4, 5, 6), c(7, 8, 9))
R List of Vectors:
# Example: List of vectors list_of_vectors <- list(c(1, 2, 3), c(4, 5, 6), c(7, 8, 9))
List Manipulation for Two-Dimensional Data in R:
# Example: List manipulation two_dim_list[[1]][[2]] <- 99
List Indexing and Subsetting in R:
# Example: List indexing and subsetting element_value <- two_dim_list[[2]][[3]]
List Operations in R for Matrices:
# Example: List operations sum_values <- sum(unlist(two_dim_list))
Converting Lists to Matrices in R:
# Example: Converting lists to matrices matrix_from_list <- matrix(unlist(two_dim_list), nrow = 3, byrow = TRUE)
R purrr Package for Working with Lists:
purrr
package provides functions for working with lists.# Example: Using purrr for lists library(purrr) modified_list <- map(two_dim_list, ~ .x * 2)
List Comprehensions in R:
# Example: List comprehensions list_comp <- lapply(1:5, function(x) x^2)
R Apply Functions for Lists:
apply
functions to perform operations on lists.# Example: Using apply functions for lists result <- lapply(two_dim_list, function(x) sum(x))
List to Data Frame Conversion in R:
# Example: List to data frame conversion data_frame_from_list <- as.data.frame(do.call(rbind, two_dim_list))
List Coercion in R:
# Example: List coercion coerced_list <- lapply(original_list, as.character)
Efficient Storage of Two-Dimensional Data in Lists in R:
# Example: Efficient storage in lists efficient_list <- vector("list", length = nrow * ncol) for (i in 1:nrow) { for (j in 1:ncol) { efficient_list[[(i - 1) * ncol + j]] <- data[i, j] } }