Skip to content

Step 1 of 5 · Reading · ~1 min

Learn

Data Frames

Vectors hold one type. Lists hold mixed types - they're R's heterogeneous container.

c() does not give you a mixed container; it coerces. c("Ada", 36, TRUE) is a character vector - the number and the logical get rewritten as the strings "36" and "TRUE" so that everything is one type. A list is the container that keeps each value as it was.

person <- list(name = "Ada", age = 36, hobbies = c("math", "engines"))
person$name              # "Ada"
person[["age"]]          # 36
person$hobbies[1]        # "math"

Note [[ ]] vs [ ]:

  • person[["age"]] returns the value (36, a number)
  • person["age"] returns a list of length 1 (a sub-list)

That distinction bites when you pass the result somewhere else: class(person["age"]) is "list", class(person[["age"]]) is "numeric".

Lists are how R handles heterogeneous returns. A lm() regression result is a list of coefficients, residuals, fitted values, etc.

Data frames (next lesson) are special lists where every element is a vector of equal length - a tabular structure.

Up nextData Frames — R's TablesData Frames

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…