13.2 Vector basics

  • There are two types of vectors:
    • Atomic vectors, of which there are six types:
      • logical
      • integer
      • double
      • character
      • complex
      • raw
    • Integer and double vectors are collectively called numeric vectors.
    • Lists, at times, are called recursive vectors because lists can contain other lists.
  • The main difference between vectors and lists:
    • Atomic vectors are homogeneous i.e., contain only one type (in my understanding).
    • Lists are heterogeneous i.e., can have different types.
  • Another related object: NULL. It is often used to represent the absence of a vector (unlike NA which is used to represent the absence of a value). NULL typically acts as a vector of length 0.

Figure 20.1: The hierarchy of R’s vector types:

  • Two key properties for each vector:
    • Its type, which can be determined with typeof().

  • Its length, which can be determined with length().
x1 <- list("a", "b", 1:10); length(x1)

x2 <- c("a", "b"); length(x2)

Qn: does this imply that the length of a list is the # of elements in a list for example (x1)?

  • Vectors can also contain arbitrary additional metadata in the form of attributes.
    • Which are then used to create augmented vectors which build on additional behaviour. There are three important types of augmented vectors:
      • Factors are built on top of integer vectors.
      • Dates and date-times are built on top of numeric vectors.
      • Data frames and tibbles are built on top of lists.