8  Lists

8.1 Definition

Lists allow you to store all types of objects and types of values: booleans, doubles, characters, vectors, other lists, data.frame, etc

# initialization
L <- list(name = "John",
          age  = 43,
          kids = list(name=c("Kevin", "Pamela"), # nested list
                      age =c(4,5)
                     )
         )
L
#> $name
#> [1] "John"
#> 
#> $age
#> [1] 43
#> 
#> $kids
#> $kids$name
#> [1] "Kevin"  "Pamela"
#> 
#> $kids$age
#> [1] 4 5
# names of entries (can be changed)
names(L)
#> [1] "name" "age"  "kids"
# statistics
summary(L)
#>      Length Class  Mode     
#> name 1      -none- character
#> age  1      -none- numeric  
#> kids 2      -none- list
str(L)
#> List of 3
#>  $ name: chr "John"
#>  $ age : num 43
#>  $ kids:List of 2
#>   ..$ name: chr [1:2] "Kevin" "Pamela"
#>   ..$ age : num [1:2] 4 5

8.2 Accessing values and other operations

L$name # is a vector
#> [1] "John"
L["age"];typeof(L["age"])     # is a list
#> $age
#> [1] 43
#> [1] "list"
L[["age"]];typeof(L[["age"]]) # is a vector
#> [1] 43
#> [1] "double"
L[[3]]      # is a list (because 'kids' is a list)
#> $name
#> [1] "Kevin"  "Pamela"
#> 
#> $age
#> [1] 4 5
L[[3]]['name']   # is a list
#> $name
#> [1] "Kevin"  "Pamela"
L[[3]][['name']] # is a vector
#> [1] "Kevin"  "Pamela"
# empty initialization
LL <- list(); LL # no specific size
#> list()
LL <- vector("list", length=3); LL # specific size
#> [[1]]
#> NULL
#> 
#> [[2]]
#> NULL
#> 
#> [[3]]
#> NULL
# Concatenation
L1 <- list(wife="Kim", wife.age=38)
L2 <- c(L, L1)
typeof(L2); L2
#> [1] "list"
#> $name
#> [1] "John"
#> 
#> $age
#> [1] 43
#> 
#> $kids
#> $kids$name
#> [1] "Kevin"  "Pamela"
#> 
#> $kids$age
#> [1] 4 5
#> 
#> 
#> $wife
#> [1] "Kim"
#> 
#> $wife.age
#> [1] 38

8.3 Exercises

  • Create a list containing 2 strings, 2 numbers, 2 vectors, 1 list and 2 logical values.
  • Give names to the elements in the list.
  • Access the first and second elements of the list.
  • Add a new item g4 = "Hello" to the list.
  • Select the second element of the nested list.
  • Remove the second element of the list.
  • Create a second list with whatever you want
  • Merge the two lists into one list.
  • Print the number of objects in the merged list.
  • Convert list(1,2,3,4) to a vector
Solution
# Create a list containing 2 strings, 2 numbers, 2 vectors, 1 list and 2 logical values.
# Give names to the elements in the list.
first_list <- list(string1 = "foo",
                   string2 = "bar",
                   number1 = 42,
                   number2 = pi,
                   vec1    = seq(-10,10,1),
                   vec2    = c("Hello", "world"),
                   list1   = list(a = 1:10, 
                                  b = 10:1),
                   bool1   = TRUE,
                   bool2   = FALSE
                  )
first_list
#> $string1
#> [1] "foo"
#> 
#> $string2
#> [1] "bar"
#> 
#> $number1
#> [1] 42
#> 
#> $number2
#> [1] 3.141593
#> 
#> $vec1
#>  [1] -10  -9  -8  -7  -6  -5  -4  -3  -2  -1   0   1   2   3   4   5   6   7   8
#> [20]   9  10
#> 
#> $vec2
#> [1] "Hello" "world"
#> 
#> $list1
#> $list1$a
#>  [1]  1  2  3  4  5  6  7  8  9 10
#> 
#> $list1$b
#>  [1] 10  9  8  7  6  5  4  3  2  1
#> 
#> 
#> $bool1
#> [1] TRUE
#> 
#> $bool2
#> [1] FALSE
# Access the first and second elements of the list.
first_list[[1]]
#> [1] "foo"
first_list[["string2"]]
#> [1] "bar"
# Add a new item `g4 = "Hello"` to the list.
first_list$g4 <- "Hello"
first_list
#> $string1
#> [1] "foo"
#> 
#> $string2
#> [1] "bar"
#> 
#> $number1
#> [1] 42
#> 
#> $number2
#> [1] 3.141593
#> 
#> $vec1
#>  [1] -10  -9  -8  -7  -6  -5  -4  -3  -2  -1   0   1   2   3   4   5   6   7   8
#> [20]   9  10
#> 
#> $vec2
#> [1] "Hello" "world"
#> 
#> $list1
#> $list1$a
#>  [1]  1  2  3  4  5  6  7  8  9 10
#> 
#> $list1$b
#>  [1] 10  9  8  7  6  5  4  3  2  1
#> 
#> 
#> $bool1
#> [1] TRUE
#> 
#> $bool2
#> [1] FALSE
#> 
#> $g4
#> [1] "Hello"
# Select the second element of the nested list.
first_list[["list1"]][[2]]
#>  [1] 10  9  8  7  6  5  4  3  2  1
# Remove the second element of the list.
first_list[-2]
#> $string1
#> [1] "foo"
#> 
#> $number1
#> [1] 42
#> 
#> $number2
#> [1] 3.141593
#> 
#> $vec1
#>  [1] -10  -9  -8  -7  -6  -5  -4  -3  -2  -1   0   1   2   3   4   5   6   7   8
#> [20]   9  10
#> 
#> $vec2
#> [1] "Hello" "world"
#> 
#> $list1
#> $list1$a
#>  [1]  1  2  3  4  5  6  7  8  9 10
#> 
#> $list1$b
#>  [1] 10  9  8  7  6  5  4  3  2  1
#> 
#> 
#> $bool1
#> [1] TRUE
#> 
#> $bool2
#> [1] FALSE
#> 
#> $g4
#> [1] "Hello"
# Create a second list with whatever you want
second_list <- list(a=1:10, b=1:10, c="hello")
# Merge the two lists into one list.
one_list <- c(first_list, second_list)
# Print the number of objects in the merged list.
length(one_list)
#> [1] 13
# Convert `list(1,2,3,4)` to a vector
as.numeric(list(1,2,3,4))
#> [1] 1 2 3 4