Advanced Data Structures
Lookup Table
Map Keys to Values
Named vectors are compact lookup tables when every value has the same type.
Program
Play the script to choose a key and retrieve the mapped status code.
lookup_table.R
key_index <-
codes <- c(ok = 200, missing = 404, error = 500)
key <- c("ok", "missing", "error")[key_index]
value <- codes[[key]]
label <- paste(key, value, sep = ":")
cat(label, "\n", sep = "")
key_index <-
codes <- c(ok = 200, missing = 404, error = 500)
key <- c("ok", "missing", "error")[key_index]
value <- codes[[key]]
label <- paste(key, value, sep = ":")
cat(label, "\n", sep = "")
key_index <-
codes <- c(ok = 200, missing = 404, error = 500)
key <- c("ok", "missing", "error")[key_index]
value <- codes[[key]]
label <- paste(key, value, sep = ":")
cat(label, "\n", sep = "")
named vector
A named vector maps character keys to same-type values.
lookup
`codes[[key]]` retrieves the value for a selected key.
map result
The final label checks both the key and returned value.