Indexing Lists

Want to learn more? I recommend working through: R for Data Science, R Cookbook, and R Graphics Cookbook.

# create a list with simulated values
score <- runif(100)
states.df <- data.frame(state1 = state.name[1:10], state2 = state.name[11:20], state3 = state.name[21:30])
name <- letters[1:20]
data.ls <- list(score, states.df, name)
rm(score, states.df, name)
# view the list
data.ls
[[1]]
  [1] 0.63621710 0.59751371 0.01766910 0.73041249 0.54653846 0.43043145
  [7] 0.11636422 0.89274444 0.44612503 0.39206699 0.12284822 0.26404920
 [13] 0.92865699 0.95513231 0.58906710 0.10746887 0.60970037 0.22867149
 [19] 0.35527089 0.60892570 0.35784036 0.72655682 0.84694322 0.39318969
 [25] 0.62687130 0.43777173 0.66495234 0.20309509 0.59805951 0.83228360
 [31] 0.03682167 0.65222574 0.39590677 0.84520655 0.65905423 0.58668714
 [37] 0.97529621 0.03153370 0.21449200 0.60840212 0.11533601 0.98922948
 [43] 0.28021083 0.93354662 0.87155782 0.13732277 0.32347877 0.41667773
 [49] 0.78557746 0.03647728 0.14362887 0.75381004 0.83698159 0.47739142
 [55] 0.49435061 0.93762428 0.76667158 0.43727024 0.92981355 0.47720586
 [61] 0.25126170 0.52683971 0.23403415 0.93919411 0.38310410 0.93215790
 [67] 0.32509680 0.46385170 0.52321614 0.11183669 0.49761109 0.12499426
 [73] 0.32904283 0.10927183 0.51750471 0.24286425 0.59768396 0.56955455
 [79] 0.86041375 0.83515259 0.20773829 0.69600639 0.83626496 0.36528740
 [85] 0.70885737 0.72743847 0.14066207 0.65039423 0.43245807 0.13238103
 [91] 0.35716796 0.24420694 0.76592652 0.43970478 0.36873838 0.48452003
 [97] 0.25268138 0.69132921 0.48043993 0.54034955

[[2]]
        state1    state2        state3
1      Alabama    Hawaii Massachusetts
2       Alaska     Idaho      Michigan
3      Arizona  Illinois     Minnesota
4     Arkansas   Indiana   Mississippi
5   California      Iowa      Missouri
6     Colorado    Kansas       Montana
7  Connecticut  Kentucky      Nebraska
8     Delaware Louisiana        Nevada
9      Florida     Maine New Hampshire
10     Georgia  Maryland    New Jersey

[[3]]
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t"
# select 1st list element
data.ls[[1]]
  [1] 0.63621710 0.59751371 0.01766910 0.73041249 0.54653846 0.43043145
  [7] 0.11636422 0.89274444 0.44612503 0.39206699 0.12284822 0.26404920
 [13] 0.92865699 0.95513231 0.58906710 0.10746887 0.60970037 0.22867149
 [19] 0.35527089 0.60892570 0.35784036 0.72655682 0.84694322 0.39318969
 [25] 0.62687130 0.43777173 0.66495234 0.20309509 0.59805951 0.83228360
 [31] 0.03682167 0.65222574 0.39590677 0.84520655 0.65905423 0.58668714
 [37] 0.97529621 0.03153370 0.21449200 0.60840212 0.11533601 0.98922948
 [43] 0.28021083 0.93354662 0.87155782 0.13732277 0.32347877 0.41667773
 [49] 0.78557746 0.03647728 0.14362887 0.75381004 0.83698159 0.47739142
 [55] 0.49435061 0.93762428 0.76667158 0.43727024 0.92981355 0.47720586
 [61] 0.25126170 0.52683971 0.23403415 0.93919411 0.38310410 0.93215790
 [67] 0.32509680 0.46385170 0.52321614 0.11183669 0.49761109 0.12499426
 [73] 0.32904283 0.10927183 0.51750471 0.24286425 0.59768396 0.56955455
 [79] 0.86041375 0.83515259 0.20773829 0.69600639 0.83626496 0.36528740
 [85] 0.70885737 0.72743847 0.14066207 0.65039423 0.43245807 0.13238103
 [91] 0.35716796 0.24420694 0.76592652 0.43970478 0.36873838 0.48452003
 [97] 0.25268138 0.69132921 0.48043993 0.54034955
# select the 1st list element, then select it's 2nd value
data.ls[[1]][2]
[1] 0.5975137
# select the 2nd list element, then select it's value in the 3rd row and 1st column
data.ls[[2]][3,1]
[1] Arizona
10 Levels: Alabama Alaska Arizona Arkansas California Colorado ... Georgia