Ordered Collections of One Type
Atomic Vector
R’s principal data structure: an ordered collection of elements of the same type. Created with the c() function (for combine).
| 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 |
typeof() returns the type of the vector.
length() returns the number of elements.
An atomic vector is like a vineyard, an ordered, 1D set of things of the same type (grapes).
: 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.
What three commands will generate the following vectors?
01:00
| Value | Meaning |
|---|---|
NULL |
does not exist |
NA |
not available / missing |
NaN |
not a number (e.g. 0/0) |
Inf |
infinity (e.g. 1/0) |
What will these return?
01:00
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.
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:
as.___(x) converts x to the type ___.
vec[index]
