Atomic Vectors

Ordered Collections of One Type

Last Time

  1. R evaluates expressions like a calculator.
  2. Functions take arguments and can have defaults.
  3. Values can be saved as objects in the environment.
  4. An R session, like the shell, has a working directory.
  5. Warnings vs. errors.

Key Ideas for Today

  1. An atomic vector holds elements of a single type.
  2. Special values mark data that is missing or undefined.
  3. Operations are vectorized and vectors are recycled.
  4. Mixing types triggers coercion.
  5. Elements are pulled out by subsetting.

Atomic Vectors

Atomic Vectors

Atomic Vector

R’s principal data structure: an ordered collection of elements of the same type. Created with the c() function (for combine).

The four common types

dbl_vec  <- c(3, 5, 1.2)
int_vec  <- c(3L, 5L, 1L)
char_vec <- c("cat", "dog", "dingo")
log_vec  <- c(TRUE, TRUE, FALSE)
Type Looks like Note
double 3, 1.2 R’s default for numbers
integer 3L the L suffix is required
character "dog" quotes required
logical TRUE, FALSE also T / F, but spell them out

Inspecting vectors

typeof() returns the type of the vector.

length() returns the number of elements.

typeof(char_vec)
typeof(log_vec)
typeof(dbl_vec)
typeof(int_vec)

length(char_vec)
length(dbl_vec)

Guess the metaphor

An atomic vector is like a vineyard, an ordered, 1D set of things of the same type (grapes).

Creating vectors quickly

Three shortcuts

: creates a sequence from the left number to the right, stepping by 1.

seq() creates a sequence from a number to a number by an interval.

rep() replicates a vector times times.

4:8
seq(from = 1, to = 3, by = .1)
seq(1, 3, .1)
rep(1:3, times = 2)

Your turn

What three commands will generate the following vectors?

[1] 5.0 5.5 6.0 6.5 7.0
[1]  2  1  0 -1 -2
[1] "cat"   "dog"   "dingo" "cat"   "dog"   "dingo"
01:00

Solutions

seq(from = 5, to = 7, by = .5)
seq(from = 2, to = -2, by = -1)
2:-2        # also works!
rep(char_vec, times = 2)

Special values

Four values that aren’t ordinary data

Value Meaning
NULL does not exist
NA not available / missing
NaN not a number (e.g. 0/0)
Inf infinity (e.g. 1/0)

Missing values are contagious

c(NA, NULL, 1)
mean(c(NA, NULL, 1))
mean(c(NA, NULL, 1), na.rm = TRUE)

Behavior of vectors

Your Turn

What will these return?

c(1, 2, 3) + c(2, 3, 4)
c(1, 2, 3) + c(2, 3)
c(1, "dog", TRUE)
01:00

Vectorization and recycling

Vectorization

Operations and functions that run on an entire vector at once, element by element.

Vector Recycling

If an operation needs a longer vector than provided, R reuses the existing elements, in order, from the beginning.

Vectorization and recycling

log(c(2, 6, 1))
c(1, 2, 3) + c(2, 3, 4)
c(1, 2, 3) + c(2, 3)

Type coercion

Implicit Coercion

When you mix types, R converts them all to a single type so the vector stays atomic. It follows a hierarchy, always moving up:

Implicit coercion

c(1, "dog", TRUE)
c(TRUE, FALSE, 3)
mean(c(TRUE, FALSE, FALSE))

Explicit coercion

as.___(x) converts x to the type ___.

distances <- c("2800", "450", "10")
distances
mean(distances)

distances <- as.double(distances)
distances
mean(distances)

Vector Subsetting

Subsetting by index

vec[index]

char_vec
char_vec[2]
char_vec[2:3]
char_vec[rep(3, 10)]

Subsetting by name

named_char_vec <- c("thrash" = "cat",
                    "buzz" = "dog",
                    "poppy" = "dingo")
named_char_vec
named_char_vec["buzz"]
named_char_vec[rep("poppy", 10)]

Subsetting by logicals

named_char_vec[c(TRUE, FALSE, TRUE)]

Key Ideas for Today

  1. An atomic vector holds elements of a single type.
  2. Special values mark data that is missing or undefined.
  3. Operations are vectorized and vectors are recycled.
  4. Mixing types triggers coercion.
  5. Elements are pulled out by subsetting.

For Next Time

  1. Work through the atomic vectors questions on PS 1.
  2. Read Advanced R, Ch. 3.1 - 3.2.
  3. Read R Coding Basics, Ch. 2–3.