DATA 220G — Week 1 - 03

R Refresher

Data Types

Lists

A data structure that lets us hold values of different modes/types

lst <- list("an element", "another elemnt", "one more")

lst
## [[1]]
## [1] "an element"
## 
## [[2]]
## [1] "another elemnt"
## 
## [[3]]
## [1] "one more"
c("an element", "another elemnt", "one more")
## [1] "an element"     "another elemnt" "one more"
list(c("an element", "another elemnt", "one more"))
## [[1]]
## [1] "an element"     "another elemnt" "one more"
list("John", "Doe", 33L, 3L, TRUE)
## [[1]]
## [1] "John"
## 
## [[2]]
## [1] "Doe"
## 
## [[3]]
## [1] 33
## 
## [[4]]
## [1] 3
## 
## [[5]]
## [1] TRUE
jdoe <- list(name = c(first = "John", last = "Doe"), age = 33L, number_of_kids = 3L, lives_in_maine = TRUE)

jdoe
## $name
##  first   last 
## "John"  "Doe" 
## 
## $age
## [1] 33
## 
## $number_of_kids
## [1] 3
## 
## $lives_in_maine
## [1] TRUE
fsmith <- list(name = c(first = "Fran", last = "Smith"), age = 23L, number_of_kids = 0L, lives_in_maine = FALSE)

fsmith
## $name
##   first    last 
##  "Fran" "Smith" 
## 
## $age
## [1] 23
## 
## $number_of_kids
## [1] 0
## 
## $lives_in_maine
## [1] FALSE
ppl <-  list(jdoe, fsmith)

ppl
## [[1]]
## [[1]]$name
##  first   last 
## "John"  "Doe" 
## 
## [[1]]$age
## [1] 33
## 
## [[1]]$number_of_kids
## [1] 3
## 
## [[1]]$lives_in_maine
## [1] TRUE
## 
## 
## [[2]]
## [[2]]$name
##   first    last 
##  "Fran" "Smith" 
## 
## [[2]]$age
## [1] 23
## 
## [[2]]$number_of_kids
## [1] 0
## 
## [[2]]$lives_in_maine
## [1] FALSE
ppl[1]
## [[1]]
## [[1]]$name
##  first   last 
## "John"  "Doe" 
## 
## [[1]]$age
## [1] 33
## 
## [[1]]$number_of_kids
## [1] 3
## 
## [[1]]$lives_in_maine
## [1] TRUE
ppl[2]
## [[1]]
## [[1]]$name
##   first    last 
##  "Fran" "Smith" 
## 
## [[1]]$age
## [1] 23
## 
## [[1]]$number_of_kids
## [1] 0
## 
## [[1]]$lives_in_maine
## [1] FALSE
ppl[1:2]
## [[1]]
## [[1]]$name
##  first   last 
## "John"  "Doe" 
## 
## [[1]]$age
## [1] 33
## 
## [[1]]$number_of_kids
## [1] 3
## 
## [[1]]$lives_in_maine
## [1] TRUE
## 
## 
## [[2]]
## [[2]]$name
##   first    last 
##  "Fran" "Smith" 
## 
## [[2]]$age
## [1] 23
## 
## [[2]]$number_of_kids
## [1] 0
## 
## [[2]]$lives_in_maine
## [1] FALSE
ppl[-1]
## [[1]]
## [[1]]$name
##   first    last 
##  "Fran" "Smith" 
## 
## [[1]]$age
## [1] 23
## 
## [[1]]$number_of_kids
## [1] 0
## 
## [[1]]$lives_in_maine
## [1] FALSE
ppl[1]$name
## NULL
ppl[[1]]$name
##  first   last 
## "John"  "Doe"
ppl[[1]]["name"]
## $name
##  first   last 
## "John"  "Doe"
#ppl[[1]][["name"]]$first

ppl[[1]][["name"]]["first"]
##  first 
## "John"
# list(1) + list(2)

list(1)[[1]] + list(2)[[1]]
## [1] 3

“The most important distinction between [, [[ and $ is that the [ can select more than one element whereas the other two select a single element.”

list(
  c("A", "B", "C"),
  1:10,
  c(TRUE, FALSE)
)
## [[1]]
## [1] "A" "B" "C"
## 
## [[2]]
##  [1]  1  2  3  4  5  6  7  8  9 10
## 
## [[3]]
## [1]  TRUE FALSE

We’ll get more into nuances of comlex lists when we work with some advanced data input. ### More info

Read these. Alot. You’ll get spot quizzes occassionally about some esoteric edge cases.

help("list")
help("[[")