Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

R Lists

R Lists

A list is a collection of objects in R that can hold different types of data, such as numbers, strings, vectors, another list, matrices, data frames, and so on; of course, it can also contain matrices and functions.

A list is a flexible data structure that can store and manipulate multiple types of data objects.

Creating a list

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

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

Example

list_data

<-

list

(

"runoob"

,

"google"

,

c

(

11

,

22

,

33

)

,

123

,

51.23

,

119.1

)

print

(

list_data

)

The output of the code above is:

[[1]]

[1] "runoob"



[[2]]

[1] "google"



[[3]]

[1] 11 22 33



[[4]]

[1] 123



[[5]]

[1] 51.23



[[6]]

[1] 119.1

We can also use the c() function to create a list, or use it 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"

)

Combine two numeric vectors

merged_vector

<-

c

(

numbers,

c

(

6

,

7

,

8

)

)

Combine two character vectors

merged_characters

<-

c

(

characters,

c

(

"grape"

,

"melon"

)

)

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

Example

The list contains vectors, matrices, and a list

list_data

<-

list

(

c

(

"Google"

,

"Runoob"

,

"Taobao"

)

,

matrix

(

c

(

1

,

2

,

3

,

4

,

5

,

6

)

,

nrow

=

2

)

,

   

list

(

"runoob"

,

12.3

)

)

Set names for the list elements

names

(

list_data

)

<-

c

(

"Sites"

,

"Numbers"

,

"Lists"

)

Display the list

print

(

list_data

)

The output of the code above is:

$Sites

[1] "Google" "Runoob" "Taobao"



$Numbers

     [,1] [,2] [,3]

[1,]    1    3    5

[2,]    2    4    6



$Lists

$Lists[[1]]

[1] "runoob"



$Lists[[2]]

[1] 12.3

Accessing a list

Elements in a list can be accessed by index; if the names() elements are named using a function, we can also access them by their corresponding names:

Example

The list contains vectors, matrices, and a list

list_data

<-

list

(

c

(

"Google"

,

"Runoob"

,

"Taobao"

)

,

matrix

(

c

(

1

,

2

,

3

,

4

,

5

,

6

)

,

nrow

=

2

)

,

   

list

(

"runoob"

,

12.3

)

)

Set names for the list elements

names

(

list_data

)

<-

c

(

"Sites"

,

"Numbers"

,

"Lists"

)

Display the list

print

(

list_data

[

1

]

)

Access the third element of the list

print

(

list_data

[

3

]

)

Access the first vector element

print

(

list_data$Numbers

)

The output of the code above is:



$Sites

[1] "Google" "Runoob" "Taobao"



$Lists

$Lists[[1]]

[1] "runoob"



$Lists[[2]]

[1] 12.3





     [,1] [,2] [,3]

[1,]    1    3    5

[2,]    2    4    6

Manipulating list elements

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

Example

The list contains vectors, matrices, and a list

list_data

<-

list

(

c

(

"Google"

,

"Runoob"

,

"Taobao"

)

,

matrix

(

c

(

1

,

2

,

3

,

4

,

5

,

6

)

,

nrow

=

2

)

,

   

list

(

"runoob"

,

12.3

)

)

Set names for the list elements

names

(

list_data

)

<-

c

(

"Sites"

,

"Numbers"

,

"Lists"

)

Adding elements

list_data

[

4

]

<-

"new element"

print

(

list_data

[

4

]

)

Delete elements

list_data

[

4

]

<-

NULL

After deletion, the output is NULL

print

(

list_data

[

4

]

)

Update an element

list_data

[

3

]

<-

"I replaced the third element"

print

(

list_data

[

3

]

)

The output of the code above is:



[[1]]

[1] "新元素"



$

NULL



$Lists

[1] "我替换来第三个元素"

When iterating over a list with a for loop:

Example

Create a list containing numbers and characters

my_list

<-

list

(

1

,

2

,

3

,

"a"

,

"b"

,

"c"

)

Use a for loop to iterate over each element in the list

for

(

element

in

my_list

)

{

 

print

(

element

)

}

In the code above, the for loop iterates over each element in the list my_list in turn and stores each element in the variable element. We can then perform specific operations on each element within the loop body, such as printing the element's value with the print() function.

When a for loop iterates over a list, each iteration assigns the current element to the variable element. Therefore, any desired operations can be performed on element within the loop body, such as calculations, conditional checks, and so on.

Note that when iterating over a list with a for loop, the loop variable element takes on each element of the list in turn, but the list elements themselves cannot be modified directly. If you need to change the value of an element in the list, you can do so by index, for example my_list[[index]] <- new_value。

The output of the code above is:

[1] 1

[1] 2

[1] 3

[1] "a"

[1] "b"

[1] "c"

Combining lists

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

Example

Create two lists

list1

<-

list

(

1

,

2

,

3

)

list2

<-

list

(

"Google"

,

"Runoob"

,

"Taobao"

)

Combining lists

merged.

list

<-

c

(

list1,list2

)

Display the combined list

print

(

merged.

list

)

The output of the code above is:

[[1]]

[1] 1



[[2]]

[1] 2



[[3]]

[1] 3



[[4]]

[1] "Google"



[[5]]

[1] "Runoob"



[[6]]

[1] "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 makes it easier to perform arithmetic operations:

Example

Creating a list

list1

<-

list

(

1

:

5

)

print

(

list1

)

list2

<-

list

(

10

:

14

)

print

(

list2

)

Convert to a vector

v1

<-

unlist

(

list1

)

v2

<-

unlist

(

list2

)

print

(

v1

)

print

(

v2

)

Add two vectors

result

<-

v1

v2

print

(

result

)

The output of the code above is:

[[1]]

[1] 1 2 3 4 5



[[1]]

[1] 10 11 12 13 14



[1] 1 2 3 4 5

[1] 10 11 12 13 14

[1] 11 13 15 17 19

Summary

The following are some commonly used R list operations and functions:

Creating a list:

Accessing list elements:

List operations:

List iteration: