YouTip LogoYouTip

R List

R Lists

Lists in R are collections of objects that can store different types of data, including numbers, strings, vectors, other lists, matrices, data frames, and even functions.

Lists are a flexible data structure that can store and manipulate multiple types of data objects.

Creating Lists

In R, lists are created using the list() function.

In the following example, we create a list containing strings, a vector, and numbers:

Example

list_data <- list("", "google", c(11,22,33), 123, 51.23, 119.1)

print(list_data)

Executing the above code produces the following output:

[]
 ""

[]
 "google"

[]
 11 22 33

[]
 123

[]
 51.23

[]
 119.1

We can also use the c() function to create lists, and this function can also be used to combine multiple objects into a single list, for example:

my_list <- c(object1, object2, object3)

Example

# Create a vector containing numbers
numbers <- c(1, 2, 3, 4, 5)

# Create a vector containing characters
characters <- c("apple", "banana", "orange")

# Merge two numeric vectors
merged_vector <- c(numbers, c(6, 7, 8))

# Merge two character vectors
merged_characters <- c(characters, c("grape", "melon"))

We can use the names() function to name the elements of a list:

Example

# List containing vectors, matrices, and lists
list_data <- list(c("Google","","Taobao"), matrix(c(1,2,3,4,5,6), nrow=2),
                 list("",12.3))

# Set names for the list elements
names(list_data) <- c("Sites", "Numbers", "Lists")

# Display the list
print(list_data)

Executing the above code produces the following output:

$Sites
 "Google"  ""  "Taobao"

$Numbers
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

$Lists
$Lists[]
 ""

$Lists[]
 12.3

Accessing List Elements

Elements in a list can be accessed using indices. If the elements have been named using the names() function, we can also access them by their corresponding names:

Example

# List containing vectors, matrices, and lists
list_data <- list(c("Google","","Taobao"), matrix(c(1,2,3,4,5,6), nrow=2),
                 list("",12.3))

# Set names for the list elements
names(list_data) <- c("Sites", "Numbers", "Lists")

# Display the list
print(list_data)

# Access the third element of the list
print(list_data)

# Access the first vector element
print(list_data$Numbers)

Executing the above code produces the following output:

$Sites
 "Google"  ""  "Taobao"

$Lists
$Lists[]
 ""

$Lists[]
 12.3

     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

Manipulating List Elements

We can add, delete, and update elements in a list, as shown in the following example:

Example

# List containing vectors, matrices, and lists
list_data <- list(c("Google","","Taobao"), matrix(c(1,2,3,4,5,6), nrow=2),
                 list("",12.3))

# Set names for the list elements
names(list_data) <- c("Sites", "Numbers", "Lists")

# Add an element
list_data <- "New Element"
print(list_data)

# Delete an element
list_data <- NULL

# After deletion, it outputs NULL
print(list_data)

# Update an element
list_data <- "I replaced the third element"
print(list_data)

Executing the above code produces the following output:

[]
 "New Element"

$<NA>
NULL

$Lists
 "I replaced the third element"

When using a for loop to traverse a list:

Example

# Create a list containing numbers and characters
my_list <- list(1, 2, 3, "a", "b", "c")

# Use a for loop to traverse each element in the list
for(element in my_list){
    print(element)
}

In the above code, the for loop traverses each element in the list my_list sequentially and stores each element in the variable element. Then, we can perform specific operations on each element within the loop body, such as printing the element's value using the print() function.

When using a for loop to traverse a list, each iteration assigns the current element to the variable element. Therefore, any necessary operations can be performed on element within the loop body, such as calculations, conditional checks, etc.

It is important to note that when using a for loop to traverse a list, the loop variable element will sequentially take on each element in the list, but it cannot directly modify the list elements themselves. If you need to modify the values of list elements, you can do so through indexing, for example, my_list[] <- new_value.

Executing the above code produces the following output:

 1
 2
 3
 "a"
 "b"
 "c"

Merging Lists

We can use the c() function to merge multiple lists into one:

Example

# Create two lists
list1 <- list(1,2,3)
list2 <- list("Google","","Taobao")

# Merge the lists
merged.list <- c(list1,list2)

# Display the merged list
print(merged.list)

Executing the above code produces the following output:

[]
 1

[]
 2

[]
 3

[]
 "Google"

[]
 ""

[]
 "Taobao"

Converting a List to a Vector

To convert a list to a vector, you can use the unlist() function. Converting a list to a vector facilitates arithmetic operations:

Example

# Create lists
list1 <- list(1:5)
print(list1)

list2 <- list(10:14)
print(list2)

# Convert to vectors
v1 <- unlist(list1)
v2 <- unlist(list2)

print(v1)
print(v2)

# Add the two vectors
result <- v1 + v2
print(result)

Executing the above code produces the following output:

[]
 1 2 3 4 5

[]
 10 11 12 13 14

 1 2 3 4 5

 10 11 12 13 14

 11 13 15 17 19

Summary

Here are some common R list operations and functions:

Creating Lists:

  • Using the c() function: For example, list1 <- c(1, 2, 3) creates a list containing 1, 2, and 3.
  • Using the list() function: For example, list2 <- list(1, "a", TRUE) creates a list containing elements of different types.

Accessing List Elements:

  • Using indices: Access elements in the list by index. For example, list1 returns the first element in the list.
  • Using element names: If the elements in the list have names, you can use the names to access them. For example, list3 <- list(a = 1, b = 2) can be accessed via list3$a and list3$b.

List Operations:

  • Length: Use the length() function to get the length of a list. For example, length(list1) returns the length of list list1.
  • Merging: Use the c() function or append() function to merge two or more lists into one. For example, list4 <- c(list1, list2) merges lists list1 and list2.
  • Adding elements: Use the c() function to add elements to an existing list. For example, list1 <- c(list1, 4) adds 4 to the end of list list1.
  • Deleting elements: Use indexing and the negative index operator - to remove elements from the list. For example, list1 <- list1 removes the second element from list list1.

List Traversal:

  • For loop: Use a for loop to traverse the elements in the list. For example, for (element in list1) { ... } traverses each element in list list1.
  • lapply() function: Applies a function to each element in the list and returns a list of results. For example, new_list <- lapply(list1, function(x) x * 2) multiplies each element in list list1 by 2.
← Php Remove SapiPhp Intdiv β†’