Learning Objectives

Vector Types

Atomic Vectors

  • Four basic types:

    • Logical: Either TRUE or FALSE
    • Integer:
      • Exactly an integer. Assign them by adding L behind it (for “long integer”).
      • -1L, 0L, 1L, 2L, 3L, etc…
    • Double:
      • Decimal numbers.
      • 1, 1.0, 1.01, etc…
      • Inf, -Inf, and NaN are also doubles.
    • Character:
      • Anything in quotes:
      • "1", "one", "1 won one", etc…
  • You create vectors with c() for “combine”

    x <- c(TRUE, TRUE, FALSE, TRUE) ## logical
    x <- c(1L, 1L, 0L, 1L) ## integer
    x <- c(1, 1, 0, 1) ## double
    x <- c("1", "1", "0", "1") ## character
  • There are no scalars in R. A “scalar” is just a vector length 1.

    is.vector(TRUE)
    ## [1] TRUE
  • Integers and doubles are together called “numerics”

    atomic vector types

  • You can determine the type with typeof().

    x <- c(TRUE, FALSE)
    typeof(x)
    ## [1] "logical"
    x <- c(0L, 1L)
    typeof(x)
    ## [1] "integer"
    x <- c(0, 1)
    typeof(x)
    ## [1] "double"
    x <- c("0", "1")
    typeof(x)
    ## [1] "character"
  • The special values, Inf, -Inf, and NaN are doubles

    typeof(c(Inf, -Inf, NaN))
    ## [1] "double"
  • Determine the length of a vector using length()

    length(x)
    ## [1] 2
  • Missing values are represented by NA.

  • NA is technically a logical value.

    typeof(NA)
    ## [1] "logical"
    • This rarely matters because logicals get coerced to other types when needed.

      typeof(c(1L, NA))
      ## [1] "integer"
      typeof(c(1, NA))
      ## [1] "double"
      typeof(c("1", NA))
      ## [1] "character"
    • But if you need missing values of other types, you can use

      NA_integer_ ## integer NA
      NA_real_ ## double NA
      NA_character_ ## character NA
    • Never use == when testing for missingness. It will return NA since it is always unknown if two unknowns are equal. Use is.na().

      x <- c(NA, 1)
      x == NA
      ## [1] NA NA
      is.na(x)
      ## [1]  TRUE FALSE
  • You can check the type with is.logical(), is.integer(), is.double(), and is.character().

    is.logical(TRUE)
    ## [1] TRUE
    is.integer(1L)
    ## [1] TRUE
    is.double(1)
    ## [1] TRUE
    is.character("1")
    ## [1] TRUE
  • Attempting to combine vectors of different types coerces them to the same type. The order of preference is character > double > integer > logical.

    typeof(c(1L, TRUE))
    ## [1] "integer"
    typeof(c(1, 1L))
    ## [1] "double"
    typeof(c("1", 1))
    ## [1] "character"
  • Exercise (from Advanced R): Predict the output:

    c(1, FALSE)
    c("a", 1)
    c(TRUE, 1L)
  • Exercise (from Advanced R): Explain these results:

    1 == "1"
    ## [1] TRUE
    -1 < FALSE
    ## [1] TRUE
    "one" < 2
    ## [1] FALSE

Attributes

Names

  • Names are a character vector the same length as the atomic vector. Each name corresponds to a single element.

  • You could set names using attr(), but you should not.

    x <- 1:3
    attr(x, "names") <- c("a", "b", "c")
    attributes(x)
    ## $names
    ## [1] "a" "b" "c"
  • Names are so special, that there are special ways to create them and view them

    x <- c(a = 1, b = 2, c = 3)
    names(x)
    ## [1] "a" "b" "c"
    x <- 1:3
    names(x) <- c("a", "b", "c")
    names(x)
    ## [1] "a" "b" "c"
  • The proper way to think about names is like this:

    names correct

    But each name corresponds to a specific element, so Hadley does it like this:

    names intuitive

  • Names stay with single bracket subsetting (not double bracket subsetting)

    names(x[1])
    ## [1] "a"
    names(x[1:2])
    ## [1] "a" "b"
    names(x[[1]])
    ## NULL
  • Names can be used for subsetting (more in Chapter 4)

    x[["a"]]
    ## [1] 1
  • You can remove names with unname().

    unname(x)
    ## [1] 1 2 3

S3 Atomic Vectors

Creating Empty Vectors

Lists

Data Frames

NULL

New Functions