Chapter 2 Vectors

Last updated: 2021-01-08 20:24:01

Aims

Our aims in this chapter are:

  • Learn how to work with R code files
  • Get to know the simplest data structure in R, the vector
  • Learn about subsetting, one of the fundamental operations with data

2.1 Editing code

2.1.1 Using code files

In Chapter 1, we typed short and simple expressions into the R console. As we progress, however, the code we write will get longer and more complex. To be able to edit and save longer code, the code is kept in code files.

Computer code is stored in code files as plain text. When writing computer code, we must use a plain text editor, such as Notepad++ or RStudio (Section 1.2). A word processor, such as Microsoft Word, is not a good choice for writing code, because:

  • Documents created with a word processor contain elements other than plain text (such as highlighting, font types, sizes and colors, etc.), which are not plain text and therefore not processed by the interpreter, leading to confusion.
  • Word processors can automatically correct “mistakes” thereby introducing unintended changes in our code, such as capitalizing: max(1)Max(1).

Any plain text file can be used to store R code, though, conventionally, R code files have the *.R file extension. Complete code files can be executed with source (Figure 2.1).

Methods of executing R code

Figure 2.1: Methods of executing R code

For example, we can run the file volcano.R which draws a 3D image of a volcano (Figure 2.2). The file is contained in the book materials, see Appendix A for the complete list of files and the download link.

3D image of the `volcano` dataset

Figure 2.2: 3D image of the volcano dataset

Download the file volcano.R from the book materials, open it in RStudio, and run the code to produce the 3D image (Figure 2.2).

Selected parts of code can be executed by marking the section and pressing Ctrl+Enter (in RStudio). Executing a single expression can be done by placing the cursor on the particular line and pressing Ctrl+Enter.

2.1.2 RStudio keyboard stortcuts

RStudio has numerous keyboard shortcuts for making it easier to edit and execute code files. Some useful RStudio keyboard shortcuts are given in Table 2.1.

Table 2.1: RStudio keyboard shortcuts
Shortcut Action
Ctrl+1 Moving cursor to the code editor
Ctrl+2 Moving cursor to the console
Ctrl+Enter Running the current selection or line
Ctrl+Shift+P Re-running the last selection
Ctrl+Alt+B Running from top to current line
Ctrl+Shift+C Turn comment on or off
Tab Auto-complete
Ctrl+D Delete line
Ctrl+Shift+D Duplicate line
Ctrl+F Find and replace menu
Ctrl+S Save

2.2 Assignment

So far we have been using R by typing expressions into the command line and observing the result on screen. That way, R functions as a “calculator”; the results are not kept in computer memory (Figure 2.3).

Simple R expressions are evaluated and printed

Figure 2.3: Simple R expressions are evaluated and printed

Storing objects in the temporary computer memory (RAM) is called assignment. In an assignment expression, we are storing an object, under a certain name, in the RAM (Figure 2.4). Assignment is done using the assignment operator. Assignment is an essential operation in programming, because it makes automation possible—reaching the goal step by step, while storing intermediate products. An assignment expression consists of:

  • The expression whose result we want to store
  • The assignment operator, = or <-
  • The name which will be assigned to the object

For example:

An assignment expression stores an object in the RAM

Figure 2.4: An assignment expression stores an object in the RAM

When we type an object name in the console, R accesses an object stored under that name in the RAM, and calls the print function on the object (Figure 2.5):

Accessing an object stored in the RAM

Figure 2.5: Accessing an object stored in the RAM

What happens when we assign a new value to an existing object? The old object gets deleted, and its name is now pointing on the new value:

Note the difference between the == and = operators! = is an assignment operator:

while == is a logical operator for comparison:

Which user-defined objects are currently in memory? The ls function returns a character vector with their names:

Why did we write ls() and not ls?

2.3 Vectors

2.3.1 What is a vector?

The vector is the simplest data structure in R, and the first we learn about in this book. A vector, in R, is an ordered collection of values of the same type, such as:

  • Numbersnumeric (numbers with a decimal point) or integer (whole numbers)
  • Textcharacter
  • Logicallogical

Recall that these are the same three types of “constant values” we saw in Chapter 1. In fact, R doesn’t have a special class for individual constant values. A constant value in R is represented by a vector of length 1.

The distinction between the numeric and integer classes (Section 1.3.11) is not very important for our purposes. Both of these two classes are used to represent numbers, and R can convert from one another automatically, as needed. We are mentioning both only because you may encounter either one when working with numbers. In this book, we will refer to both numeric and integer vectors as “numeric”, for convenience.

2.3.2 The c function

Vectors can be created with the c function, which combines the given vectors, in the specified order. For example:

Note that the c function is not restricted to combining individual values (i.e., vectors of length 1). It can be used to combine any number of vectors, of any length. For example, the following expression combines four vectors—of length 3, 1, 3 and 2—into a new vector of length 9:

Here is another example of using the c function, this time to combine four character values into a vector of length 4:

2.3.3 Vector subsetting (individual elements)

We can access individual vector elements using the [ operator and a numeric index. That way, we can get a subset with an individual vector element:

Note that numeric indices in R start at 110!

Here is another example:

Note the three components of an expression for accessing a vector element (Figure 2.6):

  • The vector being subsetted
  • Square brackets
  • The index
Components of expression to access vector elements

Figure 2.6: Components of expression to access vector elements

We can also make an assignment into a vector subset, for example to replace an individual element:

In this example, we made an assignment into a subset with a single element. As we will see later on, we can assign values into a subset of any length, using the same method (Section 2.3.9).

2.3.4 Calling functions on a vector

There are numerous functions for calculating vector properties in R. The length, min, max, range, mean and sum are most commonly used. For example:

In what way is the range function different from the other functions shown above?

Contrariwise, there are functions that operate on each vector element, separately, returning a vector of results having the same length as the input:

Why does the output of sqrt(x) contain NaN?

Note that the last expression produced a warning. A warning, in R, signals to the user that something suspicious or notable has happened. A warning differs from an error (Section 1.3.7) in that the when a warning is raised, the expression is executed nevertheless. However, when an error is raised, code evaluation stops and the expression is not executed by the interpreter.

2.3.5 The recycling rule (arithmetic)

Binary operations, such as arithmetic (Section 1.3.2) and conditional (Section 1.3.4) operators, when applied on two vectors, are done element-by-element. The result is, then, a vector of the respective results:

What happens when the input vector lengths do not match? In such case, the shorter vector gets “recycled”. For example, when one of the vectors is of length 3 and the other vector is of length 6, then the shorter vector (of length 3) is replicated two times, until it matches the longer vector (Figure 2.7). Thus, the expression:

is equivalent to the expression:

Vector recycling

Figure 2.7: Vector recycling

When one of the vectors is of length 1 and the other is of length 4, the shorter vector (of length 1) is replicated 4 times:

When one of the vectors is of length 2 and the other is of length 6, the shorter vector (of length 2) is replicated 3 times:

What happens when the longer vector length is not a multiple of the shorter vector length? In such case, recycling is “incomplete”, as indicated by a warning message:

Incomplete recycling is rarely something we want to do in practice. Therefore, the latter warning is usually an indication of something wrong in our code.

2.3.6 Consecutive and repetitive vectors

2.3.6.1 Introduction

Other than the c function (Section 2.3.2), there are three commonly used methods for creating consecutive or repetitive vectors:

2.3.6.2 Consecutive vectors

The : operator is used to create a vector of consecutive values in steps of 1:

or in steps of -1:

The seq function provides a more general way to create a consecutive vector with any step size, not necessarily 1 or -1. The three most useful parameters of the seq function are:

  • from—Where to start
  • to—When to end
  • by—Step size

For example:

The above examples of the seq function are the first case we encounter where more than one argument is passed to a function as part of a function call (Section 1.3.6). You may also have noticed that the arguments are named. We are going to elaborate on the syntax rules of using more than one argument, in a function call in R, in a moment (Section 2.3.7).

2.3.6.3 Repetitive vectors

The rep function replicates its argument to create a repetitive vector:

  • x—What to replicate
  • times—How many times to replicate

For example:

2.3.7 Function calls

Using the seq function, we will demonstrate three properties of function calls. First, we can omit parameter names as long as the arguments are passed in the default order. For example, the following two function calls are identical, because the default order of the (first three) seq function parameters is from, to and by:

Second, we can use any argument order as long as parameter names are specified. The following three function calls are identical, even though argument order is not the same, since the arguments are named:

Third, we can omit parameters that have a default argument, specified as part of the function definition. For example, the by parameter of seq has a default value of 1:

The parameters of a particular function, their order, and their default values (if any), can be found in the help file of every function (Section 1.3.12):

2.3.8 Vector subsetting (general)

So far, we created vector subsets using a numeric index which consists of a single value (Section 2.3.3), as in:

We can also use a vector of length >1 as an index. For example, the following expression returns the first and second elements of x, since the index is the the vector c(1,2) (which we create using the : operator) (Section 2.3.6.2):

Note that the vector of indices can consist of any combination of indices whatsoever. It does not have to be consecutive, and it can even include repetitions:

Here is another example (Figure 2.8):

Vector subsetting with a vector of indices (`1:3`)

Figure 2.8: Vector subsetting with a vector of indices (1:3)

And here is one more example where the index is not consecutive (Figure 2.9):

Vector subsetting with a vector of indices (`c(1:3, 7:9)`)

Figure 2.9: Vector subsetting with a vector of indices (c(1:3, 7:9))

For the next examples, let’s create a vector of all even numbers between 1 and 100:

What is the meaning of the numbers in square brackets when printing the vector?

How can we check how many elements does x have? Recall the length function (Section 2.3.4):

Using this knowledge, here are two expression that return the value of the last element in x:

Which of the last two expressions is preferable? Why?

Which index can we use to get back the entire vector? We can use an index that contains all of the vector element indices, starting from 1 and up to length(x):

What index can we use to get the entire vector except for the last two elements?

What index can we use to get a reversed vector?

Note that there is a special function named rev for reversing a vector:

Note that, when requesting an element, or elements, beyond the vector length, we get NA (Not Available) value(s). For example:

2.3.9 The recycling rule (assignment)

Earlier, we saw how the recycling rule applies to arithmetic and conditional operators (Section 2.3.5). The rule also applies to assignment.

For example, here we assign a vector of length 1 (NA) into a subset of length 6 (c(1:3,7:9)). As a result, NA is replicated six times, to match the subset:

Here, c(NA,99) is replicated three times, also to match the subset of length 6:

2.3.10 Logical vectors

2.3.10.1 Creating logical vectors

The third common type of vectors, in addition to numeric and character vectors, are logical vectors. A logical vector is composed of logical values (Section 1.3.4), TRUE and FALSE (or NA). For example:

Usually, we will not be creating logical vectors manually, but through applying a conditional operator (Section 1.3.4) on a numeric or character vector. For example:

Note how the recycling rule applies to conditional operators in the above expression.

An important property of logical vectors is, that, when arithmetic operations are applied, the logical vector is automatically converted to a numeric one, where TRUE becomes 1 and FALSE becomes 0. For example:

What is the meaning of the values 4 and 0.4 in the above example?

2.3.10.2 Subsetting with logical vectors

So far, we used a numeric vector of indices when subsetting a vector (Sections 2.3.3 and 2.3.8). A logical vector can also be used as an index for subsetting. When using a logical vector of indices, the subset contains those elements which are in the same positions as the TRUE elements in the index. This means that the logical vector of indices needs to match the length of the vector being subsetted. If this is not the case, the logical vector is recycled.

For example:

Here is another example, where the logical vector of indices is created from the same vector being subsetted:

In this example, the logical vector counts<3:

specifies whether to include each of the elements of counts in the resulting subset (Figure 2.10).

What does the expression counts[counts<3] do, in plain language?

Subsetting with a logical vector

Figure 2.10: Subsetting with a logical vector

Here are some more examples of subsetting with a logical index:

What does the output integer(0), which we got in the last expression, mean?

The next example is slightly more complex; we select the elements of z whose square is larger than 8:

Let’s go over this step-by-step. First, z^2 gives a vector of squared z values (2 is recycled):

Then, each of the squares is compared to 8 (8 is recycled):

Finally, the logical vector z^2>8 is used for subsetting z.

2.3.11 Missing values

The is.na function is used to detect missing (NA) values (Section 1.3.5) in a vector. It

  • accepts a vector, of any type, and
  • returns a logical vector, with TRUE in place of NA values and FALSE in place of non-NA values.

For example, suppose we have a vector x where some of the values are missing:

The is.na function can be used to detect which values are missing:

How can we use the above expression to subset the non-missing values of x?

Many of the functions that summarize vector properties (Section 2.3.4), such as sum and mean, have a parameter called na.rm. The na.rm parameter is used to determine whether NA values are excluded from the calculation. The default is na.rm=FALSE, meaning that NA values are not excluded. For example:

Why do we get NA in the first expression?

What do you think will be the result of length(x)?

How can we replace the NA values in x with the mean of its non-NA values?

2.4 Additional useful functions

2.4.1 any and all

Sometimes we want to figure out whether a logical vector:

  • contains at least one TRUE value; or
  • is entirely composed of TRUE values.

We can use the any and all functions, respectively, to do those things.

The any function returns TRUE if at least one of the input vector values is TRUE, otherwise it returns FALSE. For example, let’s take a numeric vector x:

The expression any(x > 5) returns TRUE, which means that the vector x > 5 contains at least one TRUE value, i.e., at least one element of x is greater than 5:

The expression any(x > 88) returns FALSE, which means that the vector x > 88 contains no TRUE values, i.e., none of the elements of x are greater than 88:

The all function returns TRUE if all of the input vector values are TRUE, otherwise it returns FALSE. For example, the expression all(x > 5) returns FALSE, which means that the vector x > 5 contains at least one FALSE value, i.e., not all elements of x are greater than 5:

The expression all(x > -1) returns TRUE, which means that x > -1 is composed entirely of TRUE values, i.e., all elements of x are greater than -1:

In a way, any and all are inverse:

  • any determines if the logical vector contains at least one TRUE value.
  • all determines if the logical vector contains at least one FALSE value.

2.4.2 which

The which function converts a logical vector to a numeric one, with the indices of TRUE values. That way, we can find out the index of values that satisfy a given condition. For example, considering the vector x:

the expression which(x > 2.3) returns the indices of TRUE elements in x > 2.3, i.e., the indices of x elements which are greater than 2.3:

2.4.3 which.min and which.max

Related to which (Section 2.4.2) are functions which.min and which.max. The latter two functions return the index of the (first!) minimal or maximal value in a vector, respectively. For example, considering the vector x:

using which.min we can find out that the minimal value of x is in the 5th position:

while using which.max we can find out that the maximal value of x is in the 2nd position:

What expression can we use to find all indices (2, 7) of the maximal value in x?

2.4.4 The order function

The order function returns ordered vector indices, based on the order of vector values. In other words, order gives the index of the smallest value, the index of the second smallest value, etc., up to the index of the largest value. For example, given the vector x:

order(x) returns the indices 1:length(x), ordered from smallest to largest value:

This result tells us that the 5th element of x is the smallest, the 6th is the second smallest, and so on.

We can also get the reverse order with decreasing=TRUE:

How can we get a sorted vector of elements from x, as shown below, using the order function?

## [1] 0 1 2 2 3 6 6

2.4.5 paste and paste0

The paste function is used to “paste” text values. Its sep parameter determines the separating character(s), with the default being sep=" " (a space). For example:

Non-character vectors are automatically converted to character before pasting:

The recycling rule applies in paste too:

The paste0 function is a shortcut for paste with sep="":


  1. Unlike Python and C, and many other programming languages, where numeric indices start at 0.