Chapter 2 Vectors

Welcome back to Quantitative Reasoning! In the previous tutorial, we explored the four different panes of the RStudio window. We also learned how to assign a single number to a variable using the assignment operator <-.

In this video, we’ll learn more about assigning values to variables. More precisely, we’ll learn how to create an R data structure called a “vector”.

Consider this example: five players rolled dice, and we want to save the results. We could store each individual result in five different variables, but this would be cumbersome. Instead we want to create a single variable, called scores, that contains all five results. The command we use is scores <- c(...), where the function c() combines the numbers that we’re going to type between the parentheses. Suppose the players rolled 5, 6, 1, 6 and 3.

scores <- c(5, 6, 1, 6, 3)

It doesn’t matter whether we leave a space after the comma. R understands what we mean with or without the space, but separating the numbers with commas is important. As we learned last time, we run this command by moving the cursor to the corresponding line. Then we click “Run”. Now the variable scores is in the environment.

In the R jargon, an object that can contain multiple elements, such as our variable scores, is called a “vector”. The abbreviation num in the Environment tab tells us that scores is a vector whose elements are numbers. We encounter vectors with other types of elements later in this tutorial. The symbol [1:5] tells us that scores contains five elements that are indexed with the integers 1 through 5.

We now learn how to access individual elements in scores. Because it isn’t important to save the next few commands, I’m going to type them into the console. But please remember that, when we want to save commands, we should type them in the editor and run the commands from there. In tutorial 04, we learn how to save the commands in the editor.

We access elements in a vector with square brackets. For example, we can view the third element in scores by typing

scores[3]

The output is 1, which is indeed the value of the third element.

We can also perform operations on individual elements. For example,

scores[3] + scores[5]

adds the third and fifth element: 1 + 3 = 4.

We can perform operations not only on individual elements, but also on entire vectors. For example, let us define another vector.

x <- c(2, 6, 3, 2, 3)

When we type

scores + x

the output is

## [1]  7 12  4  8  6

Can you spot the pattern? The first element in the output is the sum of the first elements in the two input vectors scores and x, namely 5 + 2. The second element in the output is the sum of the second elements in scores and x, namely 6 + 6. And so on.

How about scores minus x? We could explicitly type scores - x in the console. But let me show you a trick that can sometimes save you time. Press the up-arrow key once. Then we are back at the previous command we ran in the console. In general, we can go back and forth through previous commands by using the up and down arrows. Let’s go back to the previous command scores + x and replace the plus by a minus sign. The result of

scores - x
## [1]  3  0 -2  4  0

is the elementwise subtraction of x from scores. That is, the first element of the output is the first element of scores minus the first element of x. And so on.

The same elementwise arithmetic happens when we multiply. The first element in

scores * x
## [1] 10 36  3 12  9

is the first element of scores multiplied with the first element of x. And so on. Please note that the multiplication symbol in R is an asterisk. Other important elementwise arithmetic operations are division, whose symbol is a forward slash,

scores / x
## [1] 2.5000000 1.0000000 0.3333333 3.0000000 1.0000000

and exponentiation, whose symbol is a caret.

scores^x
## [1]    25 46656     1    36    27

Okay, we now know how to perform arithmetic with two different vectors, but occasionally we may want to sum all elements in one vector rather than adding elements in two vectors. For this purpose, R provides the sum() function.

sum(scores)
## [1] 21

Sometimes we want to store non-numeric objects in a vector. For example, we may want to save the names of the five players who rolled dice. Again, we use the c() function. If we want to call the vector players, the command is players <- c(), where we are going to write the players’ names between the parentheses. Suppose the players are called Rachel, Tatyana, Noah, Quentin and Aisha. We type their names in quotation marks and separate them by commas.

players <- c("Rachel", "Tatyana", "Noah", "Quentin", "Aisha")

The quotation marks are important! Otherwise R doesn’t understand what we mean. Run this command and we’ll find the variable players in the environment.

When we compare players with scores, we can see that they are of a different class. Instead of num, we see chr in the Environment tab. While num stands for “numeric”, chr stands for “character”.

We can access character vectors in the same way as numeric vectors. For example,

players[2]

returns

## [1] "Tatyana"

Be careful when mixing numbers and characters in one and the same vector. R converts the numbers to characters, and the result may not be what we intend. For example, use the up-arrow to go back to the command with which we defined x.

x <- c(2, 6, 3, 2, 3)

Now add one more element to it, say "Rachel" in quotes.

x <- c(2, 6, 3, 2, 3, "Rachel")

We can see in the Environment tab that x is no longer numeric (num), but a character vector (chr). In particular, the numbers "2", "6", "3", "2" and "3" are now all surrounded by quotation marks. This output shows that "2" is no longer the number 2, but the character "2", just as, for example, "Rachel" is composed of characters. As a consequence, arithmetic operations such as

x[1] + x[2]

no longer make sense. We cannot add words in the same way in which we can add numbers. This fact is exactly what R points out in the error message.

## Error in x[1] + x[2]: non-numeric argument to binary operator

Here the “binary operator” is the +.

You might be wondering how we can combine numeric and character data. For example, wouldn’t it make sense to have an object that lists the players’ names together with their scores? Something that resembles an Excel spreadsheet with names and scores in separate columns? We learn next time how R handles such spreadsheet-like data with objects called “data frames”. But right now, let’s summarize what we learned in this tutorial.

  • We learned how to create vectors with the c() function.
  • Vectors can be numeric or contain characters, but we cannot mix numbers and characters in the same vector.
  • We can access elements in a vector with the square bracket notation.
  • If the vector is numeric, we can perform elementwise arithmetic (plus, minus, multiplication, division and exponentiation).
  • The sum() function returns the sum of all elements in a numeric vector.

In the next tutorial, we learn how to combine multiple vectors into a data frame.

See you soon.