# Making vectors with c()
c(8, 1, 2)[1] 8 1 2
# Making vectors with :
1:3 [1] 1 2 3
# Making vectors with seq
seq(from=-1, to=11, by=3)[1] -1 2 5 8 11
Motivating scenario: You want R to be more than just a calculator dealing with one number at a time, but to deal with many measurements of the same thing (plants, traits, individuals) at one go.
Learning goals: By the end of this sub-chapter you should be able to
class() function to see the kind of vector you’re working with.
We noted previously that when using R as a calculator it returns [1] followed by the answer. This does not mean the answer is in brackets. Instead, [1] indicates the position (index) of the first element in the output vector. This is because 1 + 1 returns a vector of length one, with its first (and only) element equal to 2. The [1] tells us that the value being printed is the first element of that vector.
You can see this more clearly in Figure 1, which shows a vector containing the numbers one through ten, each squared. The first line of output begins with [1]. The second line begins with [7], indicating that the first value on that line is the seventh element of the vector. The line breaks are just for printing—they do not change the structure of the vector.
RVectors are the primary way that data is stored in R. Vectors are handy, because rather than storing each observation individually (e.g., twenty different records of petal length), we can keep them together in one collection (e.g., a vector of petal lengths).
There are many ways to make vectors in R. Below we go through three common ways we do this:
# Making vectors with c()
c(8, 1, 2)[1] 8 1 2
# Making vectors with :
1:3 [1] 1 2 3
# Making vectors with seq
seq(from=-1, to=11, by=3)[1] -1 2 5 8 11
If we want a vector of integers in increments of one, we can use a colon. E.g. 4:7 returns 4, 5, 6, 7
For more flexibility, we can use the seq() function to specify not just where the vector starts and ends, but the increase in each element. E.g. seq(from = 1.5, to = 2, by = .25) returns 1.5, 1.75, 2
For full flexibility, we use the combine function, c(), which takes arguments that are the values in the vector. E.g. c(1, 10, -2) returns 1, 10, -2.
If we observed one Clarkia plant with one flower, a second with two flowers, a third with three flowers, and a fourth with two flowers, we could find the mean number of flowers as (1 + 2 + 3 + 2)/4 = 2, but this would be tedious and error-prone. It would be easier to store these values in an ordered sequence of values (a vector) and then use the (mean()) function, as follows:
mean(x = c(1, 2, 3, 2)) # Taking the mean number of flowers per plant.[1] 2
We can also apply some simple mathematical functions to vectors, with R applying the operation to each element automatically.
# Multiplying a vector of flower numbers
# by a constant petal number
5 * c(1, 2, 3, 2) [1] 5 10 15 10
# Element-wise vector multiplication
c(3, 0.5, 1, 1) * c(1, 2, 3, 2) [1] 3 1 3 2
R handles different types of data in specific ways. Understanding these data types is crucial because what you can do with your data depends on how R interprets it. For example, although you know that 1 + "two" equals 3, R cannot add a number and a word. Understanding this will help you understand R’s error messages and confusing results when things don’t work as expected.
Other common types of vectors are:
<dbl>) and integers (<int>). Integers keep track of whole numbers, while doubles keep track of decimals (but R often stores whole numbers as doubles).<chr>, e.g., "pink" or "Clarkia xantiana").TRUE or FALSE (<logi>), often used for comparisons and conditional statements. We saw this in the first section when we asked logical questions.Note that R only allows one type of element per vector, and if you make a vector with more than one kind of element, R will make them all of the same type. The class() function reveals the kind of vector you’re dealing with. This is important, because R will act weird if the vector you give it is not of the type it expects.
class(c(1, "two"))[1] "character"
We will later see that R has many very useful objects (e.g. matrices, tibbles, lists etc), that are simply ways of organizing vectors.