Skip to Content

Subtract Two Vectors in R

In this tutorial, we will look at how to subtract two vectors in the R programming language with the help of some examples.

Vector Subtraction in R

You can use the - operator to subtract two vectors in R. Arithmetic operations on vectors are computed element-wise. That is when you subtract two vectors, the corresponding elements are subtracted together.

If the vectors are of the same length, corresponding elements (elements with the same index) are subtracted together. The following image illustrates this.

subtract two vectors of same length in R

If the vectors are of different lengths, the shorter vector will be recycled to match the length of the longer vector. The following image shows how this is done.

subtract two vectors of different lengths in R

Examples

Let’s look at some examples of subtracting two vectors in R.

Subtract two numeric vectors of the same length in R

First, let’s subtract two numeric vectors having the same length using the - operator.

# create two vectors
v1 <- c(1, 2, 3)
v2 <- c(0, 1, 2)
# subtract v2 from v1
v <- v1 - v2
# display the resulting vector
print(v)

Output:

[1] 1 1 1

Here, we subtract two vectors of length three. The resulting vector is also of length three with each element resulting from the corresponding elementwise subtraction of vector v2 from vector v1.

Subtract two numeric vectors with different lengths in R

Let’s now subtract two numeric vectors having different lengths.

# create two vectors
v1 <- c(1, 2, 3)
v2 <- c(1, 1, 1, 1, 1)
# subtract v2 from v1
v <- v1 - v2
# display the resulting vector
print(v)

Output:

Warning message in v1 - v2:
“longer object length is not a multiple of shorter object length”
[1] 0 1 2 0 1

Here, we subtract vector v2 (of length 5) from vector v1 (of length 3). You can see that the resulting vector is of length 5.

The shorter vector is recycled in order to compute the difference with the longer vector. The values 1 and 2 are recycled to the index four and five respectively. Note that we get a warning that the longer vector’s length is not a multiple of the shorter vector’s length. This is why the shorter vector was not completely recycled, only the first two values were recycled to match the length of the longer vector.

Let’s look at another example. Here, the longer vector’s length is a multiple of the shorter vector’s length.

# create two vectors
v1 <- c(1, 2, 3)
v2 <- c(1, 1, 1, 1, 1, 1)
# subtract v2 from v1
v <- v1 - v2
# display the resulting vector
print(v)

Output:

[1] 0 1 2 0 1 2

We get the resulting vector of length six (same as the length of the longer vector). Also, note that we don’t get any warnings here because the shorter vector is recycled once completely to compute the difference with the longer vector.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.