R Lists

R List is a collection of R-objects which contains elements of different types like – strings, numbers, vectors and another list inside it. A list can also contain function or a matrix as its elements.

Creating a list

In R, to create a list we have to use list() function.

Example:

Output:

Naming lists elements

We can give the name to the elements of the list. And we can also access the elements of the list using these names.

Example:

Output:

Accessing elements of the lists

We can access the elements of the list by the index of the element. Or if we are using named lists then it can also be accessed through the names.

Example:

Output:

Adding elements to a list

We can add elements at the end of the list.

Example:

Output:

Updating elements of the list

We can update any element of the list.

Example:

Output:

Deleting elements from a list

We can delete the elements of the list by assigning NULL to it.

Example:

Output:

Merging lists

We can also merge many lists into one list by placing all the lists inside one list() function.

Example:

Output:

Converting lists to vectors

We can convert the list to a vector so that the elements of a vector can be used for further operations. All the arithmetic operation like addition, subtraction, multiplication, division on vectors can be applied after the list is converted into vectors. For converting the list into a vector, we have to use unlist() function. It takes a list as input and produces a vector.

Example:

Output:

Reference:
https://www.datamentor.io/r-programming/list/