Here, we will discuss lists in R, how to create lists, and check lists.

A list contains heterogeneous data types, hence, its composition is a set of objects that can each be a combination of the numeric (or double), integer, character, logical, complex or raw data types. It has 1 or more levels or hierarchies depending on whether it simply contains a set of objects, or includes another list making it a list of lists.

1 Create Lists in R

Create a named list objects including a vector, a matrix, an array, and a dataframe:

vec1 = c("a", "b", "c")
vec1
[1] "a" "b" "c"
mat1 = matrix(c(c("a", "b"), 
                c("x", "y")),
              ncol = 2, byrow = FALSE)
mat1
     [,1] [,2]
[1,] "a"  "x" 
[2,] "b"  "y" 
arr1 = array(c("a", "b", "c", "d",
               "e", "f", "g", "h"),
             dim = c(2, 2, 2))
arr1
, , 1

     [,1] [,2]
[1,] "a"  "c" 
[2,] "b"  "d" 

, , 2

     [,1] [,2]
[1,] "e"  "g" 
[2,] "f"  "h" 
dtfrm1 = data.frame(Team = c("C", "D"), 
                   Score = c(10, 7), 
                   Position = c(3, 5))
dtfrm1
  Team Score Position
1    C    10        3
2    D     7        5
ls1 = list("vector" = vec1,
           "matrix" = mat1,
           "array" = arr1,
           "dataframe" = dtfrm1)
ls1
$vector
[1] "a" "b" "c"

$matrix
     [,1] [,2]
[1,] "a"  "x" 
[2,] "b"  "y" 

$array
, , 1

     [,1] [,2]
[1,] "a"  "c" 
[2,] "b"  "d" 

, , 2

     [,1] [,2]
[1,] "e"  "g" 
[2,] "f"  "h" 


$dataframe
  Team Score Position
1    C    10        3
2    D     7        5

Create an unnamed list of lists (a list that contains another list):

vec2 = 1:4
dtfrm2 = data.frame(Color = c("Cyan", "Yellow"), 
                   Size = c(4, 6))
dtfrm2
   Color Size
1   Cyan    4
2 Yellow    6
ls2 = list(vec2, dtfrm2, ls1)
ls2
[[1]]
[1] 1 2 3 4

[[2]]
   Color Size
1   Cyan    4
2 Yellow    6

[[3]]
[[3]]$vector
[1] "a" "b" "c"

[[3]]$matrix
     [,1] [,2]
[1,] "a"  "x" 
[2,] "b"  "y" 

[[3]]$array
, , 1

     [,1] [,2]
[1,] "a"  "c" 
[2,] "b"  "d" 

, , 2

     [,1] [,2]
[1,] "e"  "g" 
[2,] "f"  "h" 


[[3]]$dataframe
  Team Score Position
1    C    10        3
2    D     7        5

Create an empty list:

ls3 = list()
ls3
list()

Add objects to an empty list by assigning objects to some index:

ls3
list()
ls3[[1]] = dtfrm2
ls3[[4]] = vec2
# Notice that index 2 and 3 are skipped
ls3
[[1]]
   Color Size
1   Cyan    4
2 Yellow    6

[[2]]
NULL

[[3]]
NULL

[[4]]
[1] 1 2 3 4

Add or change names of objects in a list:

ls4 = ls3
names(ls4) = c("Books", "Teacher", "Class", "Room")
ls4
$Books
   Color Size
1   Cyan    4
2 Yellow    6

$Teacher
NULL

$Class
NULL

$Room
[1] 1 2 3 4

Assign values to an object in a list using index number:

First number (or set of numbers) is for the rows (horizontal), while the second number (or set of numbers) is for the columns (vertical).

# Edit the dataframe in the list
ls3
[[1]]
   Color Size
1   Cyan    4
2 Yellow    6

[[2]]
NULL

[[3]]
NULL

[[4]]
[1] 1 2 3 4
ls3[[1]][1,1] = "Blue"
ls3[[1]]$Size = c(4, 5)
ls3
[[1]]
   Color Size
1   Blue    4
2 Yellow    5

[[2]]
NULL

[[3]]
NULL

[[4]]
[1] 1 2 3 4

Assign values to an object in a list using index name:

# Edit the dataframe in the list
ls4
$Books
   Color Size
1   Cyan    4
2 Yellow    6

$Teacher
NULL

$Class
NULL

$Room
[1] 1 2 3 4
ls4$Room = c(5, 6, 7, 8)
ls4
$Books
   Color Size
1   Cyan    4
2 Yellow    6

$Teacher
NULL

$Class
NULL

$Room
[1] 5 6 7 8
Summary of the Functions to Check Lists in R
Function Usage
is.list() Check if list
typeof() Check data type
str() Check the structure of a list
  • See examples below.

2 Check Lists in R

To check if an object is a list:

ls1 = list("vector" = vec1, "matrix" = mat1)
ls1
$vector
[1] "a" "b" "c"

$matrix
     [,1] [,2]
[1,] "a"  "x" 
[2,] "b"  "y" 
is.list(ls1) # This will return TRUE or FALSE.
[1] TRUE

To check the data type or mode of a list:

This should return “list” as lists are heterogeneous.

ls2 = list("vector" = vec1, "dataframe" = dtfrm1)
ls2
$vector
[1] "a" "b" "c"

$dataframe
  Team Score Position
1    C    10        3
2    D     7        5
typeof(ls2)
[1] "list"

Using the str() function will return the data type or mode for each list object:

str(ls2)
List of 2
 $ vector   : chr [1:3] "a" "b" "c"
 $ dataframe:'data.frame':  2 obs. of  3 variables:
  ..$ Team    : chr [1:2] "C" "D"
  ..$ Score   : num [1:2] 10 7
  ..$ Position: num [1:2] 3 5

Copyright © 2020 - 2024. All Rights Reserved by Stats Codes