Vectors

An important, if not the most important, object in the R language is the vector. A vector is a set of items connected together. Building a vector is easy using the c operator:

c(1, 2, 3)
## [1] 1 2 3

This combines three items - 1 and 2 and 3 - into a vector. The same result is possible with the : (colon) operators:

1:3
## [1] 1 2 3

The two can also be combined:

c(1:3, 4)
## [1] 1 2 3 4
c(1:2, 4:5, 6)
## [1] 1 2 4 5 6
1:4
## [1] 1 2 3 4

And colon-built sequences can be in any direction:

4:1
## [1] 4 3 2 1
10:2
## [1] 10  9  8  7  6  5  4  3  2

And we can also reverse the order of a vector using rev:

1:10
##  [1]  1  2  3  4  5  6  7  8  9 10
rev(1:10)
##  [1] 10  9  8  7  6  5  4  3  2  1

Arbitrary numeric sequences can also be built with seq:

seq(from = 1, to = 10)
##  [1]  1  2  3  4  5  6  7  8  9 10
seq(2, 25)
##  [1]  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
## [24] 25

seq accepts a number of optional arguments, including: by, which controls the spacing between vector elements

seq(1, 10, by = 2)
## [1] 1 3 5 7 9
seq(0, 1, by = 0.1)
##  [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0

length.out, which controls the length of the resulting sequence

seq(0, 1, length.out = 11)
##  [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0

A related function seq_along produces a sequence the length of another vector:

seq_along(c(1, 4, 5))
## [1] 1 2 3

This is shorthand for combining seq with the length function:

length(c(1, 4, 5))
## [1] 3
seq(1, length(c(1, 4, 5)))
## [1] 1 2 3

It's also possible to create repeated sequences using rep:

rep(1, times = 5)
## [1] 1 1 1 1 1

This also allows us to repeat shorter vectors into longer vectors:

rep(c(1, 2), times = 4)
## [1] 1 2 1 2 1 2 1 2

If we use an each parameter instead of a times parameter, we can get a different result:

rep(c(1, 2), each = 4)
## [1] 1 1 1 1 2 2 2 2

Finally, we might want to repeat a vector into a vector that is not a multiple of the original vector length. For example, we might want to alternate 1 and 2 for five values. We can use the length.out parameter:

rep(c(1, 2), length.out = 5)
## [1] 1 2 1 2 1

These repetitions can be helpful when we need to categorize data into groups.

Vector classses

The above vectors are numeric, but vectors can be other classes, like character:

c("a", "b")
## [1] "a" "b"

Sequences of dates are also possible, using Date classes:

seq(as.Date("1999/1/1"), as.Date("1999/3/5"), "week")
##  [1] "1999-01-01" "1999-01-08" "1999-01-15" "1999-01-22" "1999-01-29"
##  [6] "1999-02-05" "1999-02-12" "1999-02-19" "1999-02-26" "1999-03-05"
seq(as.Date("1999/1/1"), as.Date("1999/3/5"), "day")
##  [1] "1999-01-01" "1999-01-02" "1999-01-03" "1999-01-04" "1999-01-05"
##  [6] "1999-01-06" "1999-01-07" "1999-01-08" "1999-01-09" "1999-01-10"
## [11] "1999-01-11" "1999-01-12" "1999-01-13" "1999-01-14" "1999-01-15"
## [16] "1999-01-16" "1999-01-17" "1999-01-18" "1999-01-19" "1999-01-20"
## [21] "1999-01-21" "1999-01-22" "1999-01-23" "1999-01-24" "1999-01-25"
## [26] "1999-01-26" "1999-01-27" "1999-01-28" "1999-01-29" "1999-01-30"
## [31] "1999-01-31" "1999-02-01" "1999-02-02" "1999-02-03" "1999-02-04"
## [36] "1999-02-05" "1999-02-06" "1999-02-07" "1999-02-08" "1999-02-09"
## [41] "1999-02-10" "1999-02-11" "1999-02-12" "1999-02-13" "1999-02-14"
## [46] "1999-02-15" "1999-02-16" "1999-02-17" "1999-02-18" "1999-02-19"
## [51] "1999-02-20" "1999-02-21" "1999-02-22" "1999-02-23" "1999-02-24"
## [56] "1999-02-25" "1999-02-26" "1999-02-27" "1999-02-28" "1999-03-01"
## [61] "1999-03-02" "1999-03-03" "1999-03-04" "1999-03-05"

But vectors can only have one class, so elements will be coerced, such that:

c(1, 2, "c")
## [1] "1" "2" "c"

produces a character vector

Empty vectors

We can create vectors of different classes using the appropriate functions: (1) The function numeric produces numeric vectors:

numeric()
## numeric(0)

The result is an empty numeric vector. If we supply a length parameter:

numeric(length = 10)
##  [1] 0 0 0 0 0 0 0 0 0 0

The result is a vector of zeroes. (2) The function character produces an empty character vector:

character()
## character(0)

We can again supply a length argument to produce a vector of empty chracter strings:

character(length = 10)
##  [1] "" "" "" "" "" "" "" "" "" ""

(3) The function logical produces an empty logical vector:

logical()
## logical(0)

Or, with a length parameter, a vector of FALSE values:

logical(length = 10)
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

These functions may seem kind of pointless right now. But they are useful in large projects. Filling in the values of a vector “initialized” (e.g., with numeric, character, or logical) is much faster than building a vector with c(). This is hard to observe at this scale (a few elements) but matters with bigger data.