1. Create & Print List in full and by component
m <- list(name="Mikhail", salary=75000, union=T)
m
## $name
## [1] "Mikhail"
## 
## $salary
## [1] 75000
## 
## $union
## [1] TRUE
m$salary #way_1
## [1] 75000
m[["salary"]] #way_2
## [1] 75000
m[[2]] #way_3
## [1] 75000
m$name
## [1] "Mikhail"
m$union
## [1] TRUE
  1. Create new list and several ways to add new components after list created
q2 <- list(a="bcd",b=22)
q2
## $a
## [1] "bcd"
## 
## $b
## [1] 22
q2$c <- "trading" #WAY_1 add a c component
q2
## $a
## [1] "bcd"
## 
## $b
## [1] 22
## 
## $c
## [1] "trading"
q2[[4]] <- 42 #WAY_2 add a c component
q2[5:7] <- c(T,F,F) #WAY_2 add a c component
q2
## $a
## [1] "bcd"
## 
## $b
## [1] 22
## 
## $c
## [1] "trading"
## 
## [[4]]
## [1] 42
## 
## [[5]]
## [1] TRUE
## 
## [[6]]
## [1] FALSE
## 
## [[7]]
## [1] FALSE
  1. Delete a list component
q2$b <- NULL
q2
## $a
## [1] "bcd"
## 
## $c
## [1] "trading"
## 
## [[3]]
## [1] 42
## 
## [[4]]
## [1] TRUE
## 
## [[5]]
## [1] FALSE
## 
## [[6]]
## [1] FALSE
  1. Concatenate Lists
c(list("Mike", 75000, T),list(10))
## [[1]]
## [1] "Mike"
## 
## [[2]]
## [1] 75000
## 
## [[3]]
## [1] TRUE
## 
## [[4]]
## [1] 10
  1. Get list Size
length(m) #to obtain number of components
## [1] 3
length(q2)
## [1] 6
  1. Access List Components and Values
names(m)
## [1] "name"   "salary" "union"
mu <- unlist(m) #to obtain values
mu
##      name    salary     union 
## "Mikhail"   "75000"    "TRUE"
class(mu)
## [1] "character"
  1. Access List Components and Values (example with numbers)
n <- list(a=75,b=112,c=213)
nul <- unlist(n)
class(nul)
## [1] "numeric"
nul
##   a   b   c 
##  75 112 213
  1. Access List Components and Values (mixed case example)
z <- list(a=155,b="zyx")
zul <- unlist(z)
class(zul)
## [1] "character"
zul
##     a     b 
## "155" "zyx"
  1. Calling the ‘MEDIAN’ function on each component of a list (or vector coerced to a list) and returning another list
lapply(list(10:13,325:329),median) #shows in column
## [[1]]
## [1] 11.5
## 
## [[2]]
## [1] 327
sapply(list(10:13,325:329),median) #show in line
## [1]  11.5 327.0
  1. Use the ‘lapply()’ function to retrieve observation indicies for individually for ‘M’, ‘F’, ‘I’
gen <- c("F","M","I","F","F","I","M")
lapply(c("M","F","I"),function(gender) which(gen==gender))
## [[1]]
## [1] 2 7
## 
## [[2]]
## [1] 1 4 5
## 
## [[3]]
## [1] 3 6
  1. Recursive Lists
x <- list(a = 50, b = 120)
y <- list(c = 130)
z <- list(x,y)
z
## [[1]]
## [[1]]$a
## [1] 50
## 
## [[1]]$b
## [1] 120
## 
## 
## [[2]]
## [[2]]$c
## [1] 130
length(z)
## [1] 2
  1. Recursive Lists (using concatenate function ‘c()’ with optional argument recursive)
c(list(a=1,b=2,c=list(d=5,e=9))) #two-component list (each component itself also a list)
## $a
## [1] 1
## 
## $b
## [1] 2
## 
## $c
## $c$d
## [1] 5
## 
## $c$e
## [1] 9
c(list(a=1,b=2,c=list(d=5,e=9)),recursive=T) # optional argument recursive controls flattening
##   a   b c.d c.e 
##   1   2   5   9