# Operations on Vectors in R

Vectors in R is the native way of handling data. In addition to the vector operations you saw in the linear algebra textbook, R supports a lot more. In this post, you will learn about:

• How to manipulate a vector
• How to treat vectors as sets

Let’s get started. Operations on Vectors in R
Photo by Pablo García Saldaña. Some rights reserved.

## Overview

This post is divided into three parts; they are:

• Reading and Modifying a Vector
• Vectors as Sets
• Arrays in R

## Reading and Modifying a Vector

The simplest vector in R is an integer vector created as follows:

This provides the two endpoints of a sequence of consecutive integers and a vector is created. For more complicated vectors, you can use the seq() function, which is to generate an arithmetic progression:

or for an arbitrary sequence of numbers, use the c() function:

Given a long vector, you can always extract one element from it by providing an index:

The above prints the first element of the vector x. Note that in R, indices start at 1.

The same syntax supports multiple indices. For example, to get the first 5 elements of the vector x, you use:

This works because 1:5 is 1, 2, 3, 4, 5. These will be the indices to access the vector x. The result will be the five elements at the corresponding positions of x, formed as a new vector. Because 1:5 is just a vector, you can also do:

which can be reasoned similarly.

Updating an element of a vector is as simple as an assignment:

But if you want to insert new elements to a vector instead of overwriting existing ones, you need to use the append() function:

Indeed, append() function works not only at the end of the vector, but also anywhere in the middle:

Note that the R index starts at 1, so “after=0” above means inserting the element at the beginning. In fact, inserting an element at the beginning is same as concatenating a single-element vector to another vector. Hence you can use the following syntax as well:

As a side note, the function c() in R stands for “combine”. This function can help you concatenate multiple vectors into one.

Also note that almost everything in R is immutable. The append operation creates a new vector. Hence you need to reassign to the vector to update it.

To remove an element from a vector, you need to do this:

This removed the element of x from x. But since vectors are immutable in R, this creates a new vector instead of updating it. Hence you need to store it in a variable to keep the result.

To remove multiple elements of a vector, you can use the negative of a vector of indices:

In summary, R treats negative indices as removal requests.

## Vectors as Sets

Vectors are the fundamental data objects in R. Mathematically, vectors are ordered tuples, which the order of their elements matters. Unordered tuples are sets. In R, we can use vectors as sets.

Let’s consider the following example:

Here four vectors are defined. You can tell that w and x are identical vectors:

which returns TRUE. But x and z are not identical in the vector sense. They are the same set, however, since you can tell from

Indeed, setequal() will report TRUE for vector of unequal size as long as the unique elements match perfectly, since sets don’t consider repeated elements.

As you can expect, you can produce a new vector in the sense of a set union and set intersection:

In addition, you can also find the set difference:

These two produces different result because set difference is to remove elements from the first set whenever it appears in the second set.

Considering a vector as a set, you can now check if something is a member:

Unfortunately, there is no “not in” operator in R. You need to negate the Boolean value to mean that:

## Arrays in R

You have seen how we can convert a vector into a matrix in the previous post. Matrices are 2D representations of data. If you need a higher dimension, you need an array.

To convert a vector into an array, you can use the syntax similar to matrix, except that you need to explicitly specify the dimensions (and make sure the number of elements in the vector match):

In R, an array is filled along the first dimension, then the second dimension, and so on. Hence in the above, vector 2:25 is filled into array x in such a way that x[,1,1] is c(2,3,4) and x[,2,1] is c(5,6,7).

Similar to vector indexing, with such 3D array, you can extract a 2D array from it using the following syntax:

Because x[c(1,2), 1, 1] is c(2,3) and x[c(1,2), 2, 1] is c(5,6), the above will give you:

$$\begin{matrix} 2 & 5 \\ 3 & 6 \\ \end{matrix}$$

To get back a vector from an array, you can simply “cast” its type:

## Summary

In this post, you learned about some useful operations on vectors and arrays in R. Specifically, you know

• How to perform splicing on vectors
• How to use vectors as sets
• How to use multi-dimensional arrays in R