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.

## 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:

1 |
x <- 2:100 |

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:

1 |
y <- seq(from=2, to=100, by=1) |

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

function:

1 |
y <- c(2, 3, 5, 8, 13, 21, 34) |

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

1 |
print(x[1]) |

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:

1 |
print(x[1:5]) |

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:

1 |
print(x[c(2,3,5,7)]) |

which can be reasoned similarly.

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

1 2 |
x[4] <- 42 x[4:5] <- c(42, 43) |

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

function:

1 2 |
x <- append(x, 101) x <- append(x, c(102, 103)) |

Indeed, `append()`

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

1 |
x <- append(x, -1, after=0) |

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:

1 |
x <- c(-1, x) |

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:

1 |
x <- x[-3] |

This removed the element of `x[3]`

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:

1 2 |
x <- x[-c(2,4,6,8)] x <- x[-(2:5)] |

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:

1 2 3 4 |
w <- c("apple", "banana", "cantaloupe") x <- c("apple", "banana", "cantaloupe") y <- c("banana", "pear", "tangerine") z <- c("cantaloupe", "banana", "apple") |

Here four vectors are defined. You can tell that `w`

and `x`

are identical vectors:

1 |
identical(w, x) |

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

1 |
setequal(x, z) |

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:

1 2 |
intersect(x,y) union(x,y) |

In addition, you can also find the set difference:

1 2 |
setdiff(x, y) setdiff(y, x) |

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:

1 |
“apple” %in% x # TRUE |

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

1 |
!(“apple” %in x) # FALSE |

## 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):

1 |
x <- array(2:25, dim=c(3,2,4)) |

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:

1 |
x[c(1,2), c(1,2), 1] |

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:

1 |
as.vector(x) |

## Further Readings

You can learn more about the above topics from the following:

### Web site

- The R Manuals: https://cran.r-project.org/manuals.html

### Books

## 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

## No comments yet.