Vectors: c(), seq(), rep() and length()
Lesson 2 of 8 · 11 min
Everything is a vector
The [1] you saw in the first lesson is the clue: R stores every value as a vector, an ordered row of elements of one kind. A single number is a vector of length one. c() combines values into a longer vector, and length() tells you how many elements it holds. You can also combine an existing vector with new values, which is how you append.
temps <- c(21.5, 23.1, 19.8, 24.6, 22.0)
temps
#> [1] 21.5 23.1 19.8 24.6 22.0
length(temps)
#> [1] 5
c(temps, 18.4)
#> [1] 21.5 23.1 19.8 24.6 22.0 18.4
days <- c("Mon", "Tue", "Wed")
days
#> [1] "Mon" "Tue" "Wed"
length(days)
#> [1] 3Text values need quotation marks. Without them, R reads Mon as the name of an object and reports that no such object exists. Notice also that c(temps, 18.4) printed the longer vector but did not change temps; to keep the result you would assign it, for example temps <- c(temps, 18.4).
Regular sequences: the colon and seq()
Typing consecutive integers by hand is error-prone, so R has two tools. The colon operator a:b produces every integer from a to b, in either direction. seq() is the general version: you give a start, an end, and either a step size with by or the number of elements you want with length.out.
1:10 #> [1] 1 2 3 4 5 6 7 8 9 10 5:1 #> [1] 5 4 3 2 1 seq(0, 100, by = 25) #> [1] 0 25 50 75 100 seq(1, 2, length.out = 5) #> [1] 1.00 1.25 1.50 1.75 2.00 seq(10, 1, by = -3) #> [1] 10 7 4 1 length(seq(0, 1, by = 0.05)) #> [1] 21
The last line is a classic fencepost: from 0 to 1 in steps of 0.05 there are 20 steps but 21 values, because both ends are included. Whenever you build a grid with seq(), check length() before relying on it. A sequence that would overshoot its end simply stops early; seq(10, 1, by = -3) ends at 1 because the next value, -2, lies beyond the limit.
Repetition with rep()
rep() repeats a value or a vector. The two arguments beginners mix up are times, which repeats the whole vector end to end, and each, which repeats every element in place before moving to the next.
rep(0, 4)
#> [1] 0 0 0 0
rep(c("a", "b"), times = 3)
#> [1] "a" "b" "a" "b" "a" "b"
rep(c("a", "b"), each = 3)
#> [1] "a" "a" "a" "b" "b" "b"times gives an alternating pattern, useful for labelling paired measurements; each gives blocks, useful for labelling groups that were measured one after another. Both are common when you type in a small dataset and need a group label next to each value.
Long vectors and the index hints
When a vector does not fit on one line, R wraps it and starts every line with the index of its first element in square brackets. Those numbers are not part of the data; they are there so you can find element 20 without counting.
x <- 100:130 x #> [1] 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 #> [20] 119 120 121 122 123 124 125 126 127 128 129 130
One vector, one kind of value
A vector holds numbers, or text, or logical values, never a mixture. If you write c(1, "two", 3), R cannot store a word among numbers, so it silently turns every element into text and the result is "1" "two" "3". The arithmetic you will learn next fails on such a vector. The course Data Structures covers these conversions in detail; for now, keep numbers and words in separate vectors.
rep(c(1, 2), each = 2) produce?Sign in to answer and track your progress.
Sign in