Lists in R Programming Language

Rumman Ansari   Software Engineer   2023-03-24   5994 Share
☰ Table of Contents

Table of Content:


List is a data structure having components of mixed data types.

A vector having all elements of the same type is called atomic vector but a vector having elements of different type is called list.

We can check if it’s a list with typeof() function and find its length using length(). Here is an example of a list having three components each of different data type.

> x
$a
[1] 2.5
$b
[1] TRUE
$c
[1] 1 2 3
> typeof(x)
[1] "list"
> length(x)
[1] 3

Create a list in R programming

List can be created using the list() function.

> x <- list("a" = 2.4, "b" = TRUE, "c" = 1:3)

Here, we create a list x, of three components with data types doublelogical and integervector respectively.

Its structure can be examined with the str() function.

> str(x)
List of 3
$ a: num 2.4
$ b: logi TRUE
$ c: int [1:3] 1 2 3

In this example, ab and c are called tags which makes it easier to reference the components of the list.

However, tags are optional. We can create the same list without the tags as follows. In such scenario, numeric indices are used by default.

> x <- list(2.4,TRUE,1:3)
> x
[[1]]
[1] 2.4
[[2]]
[1] TRUE
[[3]]
[1] 1 2 3

How to access components of a list?

Lists can be accessed in similar fashion to vectors. Integer, logical or character vectors can be used for indexing. Let us consider a list as follows.

> x
$name
[1] "John"
$age
[1] 19
$speaks
[1] "English" "French" 
> x[c(1:2)]    # index using integer vector
$name
[1] "John"
$age
[1] 19
> x[-2]        # using negative integer to exclude second component
$name
[1] "John"
$speaks
[1] "English" "French" 
> x[c(T,F,F)]  # index using logical vector
$name
[1] "John"
> x[c("age","speaks")]    # index using character vector
$age
[1] 19
$speaks
[1] "English" "French" 

Indexing with [ as shown above will give us sublist not the content inside the component. To retrieve the content, we need to use [[.

However, this approach will allow us to access only a single component at a time.

> x["age"]
$age
[1] 19
> typeof(x["age"])    # single [ returns a list
[1] "list"
> x[["age"]]    # double [[ returns the content
[1] 19
> typeof(x[["age"]])
[1] "double"

An alternative to [[, which is used often while accessing content of a list is the $ operator. They are both the same except that $ can do partial matching on tags.

> x$name    # same as x[["name"]]
[1] "John"
> x$a                  # partial matching, same as x$ag or x$age
[1] 19
> x[["a"]]             # cannot do partial match with [[
NULL
> # indexing can be done recursively
> x$speaks[1]
[1] "English"
> x[["speaks"]][2]
[1] "French"

Modify a list in R

We can change components of a list through reassignment. We can choose any of the component accessing techniques discussed above to modify it.

Notice below that modification causes reordering of components.

> x[["name"]] <- "Clair"; x
$age
[1] 19
$speaks
[1] "English" "French" 
$name
[1] "Clair"

Add components to a list

Adding new components is easy. We simply assign values using new tags and it will pop into action.

> x[["married"]] <- FALSE
> x
$age
[1] 19
$speaks
[1] "English" "French" 
$name
[1] "Clair"
$married
[1] FALSE

Delete components from a list

We can delete a component by assigning NULL to it.

> x[["age"]] <- NULL
> str(x)
List of 3
$ speaks : chr [1:2] "English" "French"
$ name   : chr "Clair"
$ married: logi FALSE
> x$married <- NULL
> str(x)
List of 2
$ speaks: chr [1:2] "English" "French"
$ name  : chr "Clair"

Operation on a list


 theList <- list(A = matrix(1:9, nrow = 3), B = 1:5, c = matrix(1:2, nrow = 2), D = 2)
 theList

 # this will return a list
 lapply(theList, sum) 

 # this will return a vector
 sapply(theList, sum) 
 
 # another application
 
 theNames <- c("Rumman", "Azam", "Master", "Inza")
 lapply(theNames, nchar)
 

If you will run the above code it will produce the below output


>  theList <- list(A = matrix(1:9, nrow = 3), B = 1:5, c = matrix(1:2, nrow = 2), D = 2)
>  theList
$A
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

$B
[1] 1 2 3 4 5

$c
     [,1]
[1,]    1
[2,]    2

$D
[1] 2

> 
>  # this will return a list
>  lapply(theList, sum) 
$A
[1] 45

$B
[1] 15

$c
[1] 3

$D
[1] 2

> 
>  # this will return a vector
>  sapply(theList, sum) 
 A  B  c  D 
45 15  3  2 
>  
>  # another application
>  
>  theNames <- c("Rumman", "Azam", "Master", "Inza")
>  lapply(theNames, nchar)
[[1]]
[1] 6

[[2]]
[1] 4

[[3]]
[1] 6

[[4]]
[1] 4


Another Example


# create a first list
firstList <- list(A = matrix(1:16, nrow = 4),
               B = matrix(1:16, nrow = 2),
               C = 1:5)

secondList <- list(A = matrix(1:16, nrow = 4),
                B = matrix(1:16, nrow = 8),
                C = 1:15)

firstList
secondList
mapply(identical, firstList, secondList)


# create a nice function

simpleFun <- function(x,y)
{
  NROW(x) + NROW(y)
}

mapply(simpleFun, firstList, secondList)


Output


> # create a first list
> firstList <- list(A = matrix(1:16, nrow = 4),
+                B = matrix(1:16, nrow = 2),
+                C = 1:5)
> 
> secondList <- list(A = matrix(1:16, nrow = 4),
+                 B = matrix(1:16, nrow = 8),
+                 C = 1:15)
> 
> firstList
$A
     [,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]    2    6   10   14
[3,]    3    7   11   15
[4,]    4    8   12   16

$B
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    3    5    7    9   11   13   15
[2,]    2    4    6    8   10   12   14   16

$C
[1] 1 2 3 4 5

> secondList
$A
     [,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]    2    6   10   14
[3,]    3    7   11   15
[4,]    4    8   12   16

$B
     [,1] [,2]
[1,]    1    9
[2,]    2   10
[3,]    3   11
[4,]    4   12
[5,]    5   13
[6,]    6   14
[7,]    7   15
[8,]    8   16

$C
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15

> mapply(identical, firstList, secondList)
    A     B     C 
 TRUE FALSE FALSE 
> 
> 
> # create a nice function
> 
> simpleFun <- function(x,y)
+ {
+   NROW(x) + NROW(y)
+ }
> 
> mapply(simpleFun, firstList, secondList)
 A  B  C 
 8 10 20 
>