 # List Data Structure in R

February 8, 2022, Learn eTutorial
1078

In this tutorial, you will learn about the list data structure in R. How to create a list, the functions supported by the list, etc.

## What is a list in R programming?

In R, a list is a data structure of generic vectors containing heterogeneous data type elements. In simple words, a list is an object which contains elements of different data types like characters, numerical, logical, vectors, etc. A list is one-dimensional with an ordered collection of different (heterogeneous) data type elements.

A list contains a collection of basic data types like numeric, integer, character type elements to form a heterogeneous data structure. Like in other programming languages, the R programming language provides indexing of each element that constitutes a list. In the list, indexing begins with 1 as in vectors in R which are used for accessing elements or for some list element manipulation operations.

## How to create a list in R Programming?

In R a list is created using a function list().

#### The syntax for creating the list in R Programming

``````
Name_of_list = list(mention_list_elements)
```
```

Consider an example of the Students record with Student name, roll number, section, the item they participate in an event. We know a list is a generic vector object. In other words, a list is a collection of vectors.

``````
Student_Name = c("Alliet","James","Sarah")
Student_Rollno = c(3,11,19)
class = c("X") secti
item = c("Singing","Playing","Dance")
```
```

Here 5 vectors such as Student_Name , Student_Rollno , class , section , item are created using c() function (link to vector) and they are of different data type elements. The vector Student_Name is of character data type elements, vector Student_Rollno is of numeric data type elements and so on.

A list is a data structure that supports to include all these different (heterogeneous) vectors created to organize under one structure. The student_list is created using list() function. The parentheses () include all the vectors that constitute a list such as Student_Name, Student_Rollno, class, section, item as shown below

``````
student_list = list(Student_Name,Student_Rollno,class,section,item)        # created list using list()
```
```

Now let us see the result produced after executing these codes together

### Program to create a list using list() function

``````
Student_Name = c("Alliet","James","Sarah") #vectors are created of character, numeric data types
Student_Rollno = c(3,11,19)
class = c("X") secti
item = c("Singing","Playing","Dance")
student_list = list(Student_Name,Student_Rollno,class,section,item)    #a list is created using list()
print(student_list)    #prints the result

```
```

When the code is executed it displays a list (student_list )of vectors.

Output:

```
[]
 "Alliet" "James"  "Sarah"

[]
  3 11 19

[]
 "X"

[]
 "A" "B" "B"

[]
 "Singing" "Playing" "Dance"

``` ## List functions in R

As we discussed in our previous tutorial, the list also supports functions like `typeof()`,`length()`, `is.list()`.

Function Description R code Output
typeof() Determines the list data type `typeof(student_list)`

> typeof(student_list)

 "list"

length() To check list length `length(student_list)`

> length(student_list)

 3

is.list() To check list length `is.list(student_list)`

> is.list(student_list)

 TRUE>

## Diffrence between vector and list in R

Let us see a table to compare the difference between vector and list.

Vector List
A vector is a homogeneous data structure. A list is a heterogeneous data structure.
A vector is 1D.(one dimensional) A list is multidimensional.
It is created using c(). It is created using list().
Example :
``````
> Name = c("Alliet","James","Sarah")
> Name
 "Alliet" "James" "Sarah"

> Rollno = c(3,11,19)
> Rollno
 3 11 19

> class = c("X")
> class
 "X"
```
```
We created three vectors Name, Rollno, class
Example :
``````
> Stu_list = list(Name,Rollno,class)

> Stu_list
[]
 "Alliet" "James" "Sarah"

[]
 3 11 19

[]
 "X"
```
```
A list Stu_list is created with three vectors Name, Rollno, class with index 1,2,3.

Note : In R programming both vectors and list indexing begins with 1 unlike other languages such as C, jAVA ,python etc.

Thus a list is a collection of vectors of heterogeneous data elements. A list can also contain numeric, character, logical basic data types other than vector types .Above example we discussed list consist of vector data structure. Let's create a list of elements containing numeric, logical, integer and vector data types:

``````
a <- list(site = "Learn eTutorials", Lang ="R ", Year = 2022, tutorials =c("GO","PYTHON","DATA SCIENCE"))
```
```

Output:

```
> a <- list(site = "Learn eTutorials",Lang ="R ", Year = 2022,tutorials =c("GO","PYTHON","DATA SCIENCE"))
> a
\$site
 "Learn eTutorials"

\$Lang
 "R "

\$Year
 2022

\$tutorials
 "GO"           "PYTHON"       "DATA SCIENCE"
```

## Different Ways To Create R Programming List

There are two ways/methods to create a list

1. In a single row by mentioning the variable name & corresponding data type
2. The elements or objects are declared in multiple lines . Then the list is created by specifying the objects inside list parentheses.
Creating list in single line with declaration of variables Declaring variables in multiple lines & Creating list
``````
A = list(A = 1,B=c("hello","haii"),D =TRUE)
```
```
``````
A = 1
B=c("hello","haii")
D =TRUE
A = list(A,B,D)
```
```
Output
```
\$A
 1

\$B
 "hello" "haii"

\$D
 TRUE
```
Output
```
[]
 1

[]
 "hello" "haii"

[]
 TRUE
```
Along with the Index the output is displayed. [] [] []

## How to create named list in R Programming?

You can give names to list elements in two ways in R programming.

1. Names to List elements can be specified while creating a list itself.
2. Using the names() function.

#### The syntax for naming the elements while creating a list itself

``````
NEW_List = list ("name1" = <list _element>,"name2 "= ,….)
```
```
``````
A = 1
B=c("hello","haii")
C =TRUE
NEW_List = list("first" = A,"Second"=B ,"Third"=C)
print(NEW_List)
print(NEW_List\$Third)
```
```

The NEW_List is the created list with names given as “first”,”second”,”third” corresponding to each elements in list.

The corresponding output after naming list element is

```
\$first
 1

\$Second
 "hello" "haii"

\$Third
 TRUE
```

## How to name List elements using the names() function in R?

Let us create a name for the above-discussed program using the `names()` function.List elements (vector) can be created by c() function which assigns new names corresponding to each position of list elements.

``````
names(NEW_List)=c("name1","name2","name3")
```
```

Let us check the whole R source code to understand how we can give the names to the list elements

``````
A = 1
B=c("hello","haii")
C =TRUE
# Creating a list containing a vector, a matrix and a list.
NEW_List = list(A,B ,C)
# Giving names to the elements in the list.
names(NEW_List)=c("first" ,"Second" ,"Third")
# Show the list.
print(NEW_List)
```
```

Here first in first position gives name to A = 1, the name Second is assigned to B=c("hello","haii") and so on.

The corresponding output after naming list element is

```

\$first
 1

\$Second
 "hello" "haii"

\$Third
 TRUE
```

Let us see the difference between two approaches

Giving names while creating list Giving name using names()

Syntax :

`NEW_List = list("first" = A,"Second"=B ,"Third"=C)`

Syntax :

`names(NEW_List)=c("first" ,"Second" ,"Third")`

Output

\$first  1

\$Second  "hello" "haii"

\$Third  TRUE

Output

\$first  1

\$Second  "hello" "haii"

\$Third  TRUE

## How to access list elements in R Programming?

You can access elements from a list using the index of elements or by their names in the list ie by using the position numbers of values or element names. Look at the above table example and output for code

### Access List Elements using Index

``````
A = 1
B=c("hello","haii")
D =TRUE
A = list(A,B,D)
print(A)
print(A)
print(A)

```
```

It is easy to access elements by providing index numbers inside square brackets along with the name of the list from which element needs to access in a `print()` function.

```
> print(A)
[]
 1

> print(A)
[]
 "hello" "haii"

```

### Access List Elements using \$ and list element name

``````
A = 1
B=c("hello","haii")
C =TRUE
NEW_List = list("first" = A,"Second"=B ,"Third"=C)
print(NEW_List)
# Access by names
cat("Access element by name using \$ command\n")
print(NEW_List\$Third)
```
```

Output:

```
\$first
 1

\$Second
 "hello" "haii"

\$Third
 TRUE

Access element by name using \$ command
 TRUE
>
```

## How to add an element to List in R?

A new element can be added to a list by specifying the name of variable or vector type defined with the next preferred index number within square brackets and the value that needs to assign to it.

In the previous code we created a list with index value 3. Let us add a new element to the list.

``````
A= "NEW ELEMENT"
print(A)
```
```

Let us check how the addition of a new element gets executed with above piece of code

``````
A = 1
B=c("hello","haii")
D =TRUE
A = list(A,B,D)
print(A)

A= "NEW ELEMENT"
print(A)
```
```

We added a new element at index 4.Lets us see the change occurred in list A after addition of new element. Use print(A) function to display or view the whole list.

```
[]
 1

[]
 "hello" "haii"

[]
 TRUE

[]
 "NEW ELEMENT"
```

## How to remove an element in the list in R?

In R list elements are removed by indicating a negative sign upon the index of the element which needs to be removed/delete from list.

``````
print(Variable_name[-<index>])
```
```

### program to remove list element

``````
Student_Name = c("Alliet","James","Sarah")
Student_Rollno = c(3,11,19)
class = c("X") secti
item = c("Singing","Playing","Dance")
student_list = list(Student_Name,Student_Rollno,class)
print(student_list)
print(student_list[-1])
```
```

When the code `print(student_list[-1])` gets executed it results in removal of value at index 1.The list will contain only the elements Student_Rollno, class.

Output:

```
> print(student_list)
[]
 "Alliet" "James"  "Sarah"

[]
  3 11 19

[]
 "X"

> print(student_list[-1])
[]
  3 11 19

[]
 "X"

>
```

## How to update a list element in R?

You can easily update or modify list element values by mentioning the index of the element in the list that needs to be modified or updated.

These codes update elements or values by providing index values.

``````
student_list[]= 14
#updated with a new value at second position of the 2nd index element in list
student_list[]="Williams"          #added element at third position of first index element in list
student_list[]="Stephen"          #updated with new name

print(student_list)
```
```

When these codes get executed `student_list[]= 14`

the second index of the list ie, Student_Rollno is identified and within Student_Rollno the element present at index 2 or second position is updated from 11 to 14. The value to which updation or modification is required is assigned to the corresponding index position.

```
[]
 "Alliet"  "James"   "Stephen"

[]
  3 14 19

[]
 "X"
```

You can see that `Student_Name = c("Alliet","James”)`  gets updated to "Alliet"  "James"   "Stephen". Similarly  `Student_Rollno = c(3,11,19)`  modified as  3 14 19.

## What is an `str()` function in the R  list ? or How to check the structure of a list?

In R the internal structure of a list is displayed using `str()` function.

The internal structure of the student_list of our previous examples is checked using `str()` function. The command str(student_list)  displays  the list  structure as given below

You can infer from the output the `str()` function displays the contents of list elements with index range and their basic data types.

## How to do/perform concatenation in the R list?  Or how to merge the list in R Programming?

Lists can be concatenated in the R list using the following syntax

``````
New_list =c(list1,list2,…)
```
```
``````
a =list("R",7,x=c(3,4))
b= list(y=c(TRUE,FALSE,FALSE),Z=34L)
NEW_LIST =c(a,b)
print(NEW_LIST)
```
```

Output:

```
[]
 "R"

[]
 7

\$x
 3 4

\$y
  TRUE FALSE FALSE

\$Z
 34

>
```

## How to convert a list to vector data structure in R?

The unlist() function is used to convert a list to vector in R.

``````
unlist(list_name)
```
```

### Program to convert list to vector

``````
a =list("R",7,x=c(3,4))   #CREATED A LIST
b =unlist(a)              #unlist converts list to vector
print(b)
```
```

Output:

```
x1  x2
"R" "7" "3" "4"
```

## Summary of List Data Structure

function Description
list() To create a list
names() To attach names to a list
typeof() Determines the list data type
length() To check list length
is.list() To check existence of  a list
str() To check internal structure of a list
unlist() To convert list to vector.