typestar

Lists in R

Heterogeneous containers, the shape of every R model object.

model <- list(
  name = "linear",
  coefficients = c(intercept = 1.5, slope = 0.8),
  converged = TRUE
)

name <- model$name
coefs <- model[["coefficients"]]
subset <- model["converged"]

model$rss <- 12.4
fields <- names(model)
flat <- unlist(model$coefficients)

How it works

  1. $ and [[ ]] extract an element's value.
  2. Single brackets return a sub-list instead.
  3. Assigning a new name grows the list.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
257
Tokens
64
Three-star pace
90 tpm

At the three-star pace of 90 tokens a minute, this run takes about 43 seconds.

Type this snippet

Step 2 of 2 in Matrices & lists, step 16 of 20 in Language basics.

← Previous Next →