## Applying arithmetic operators on vectors

___ + ___
___ - ___
___ / ___
___ * ___
View Interactive Version

Operations, such as addition, subtraction, multiplication and division are called arithmetic operations. They can not only operate with single values but also with vectors. If you use arithmetic operations on vectors, the operation is done on each individual number from the first vector and the individual number at the same position from the second vector.

In the following example we create two numeric vectors and assign them to the variables a and b. We then add them together:

Input
a <- c(1, 3, 6, 9, 12, 15)
b <- c(2, 4, 6, 8, 10, 12)
a + b
Result
  3  7 12 17 22 27

As the output shows, the first elements of the two vectors were added together and resulted in 1 + 2 = 3. The second elements added up to 3 + 4 = 7, the third elements to 6 + 6 = 12 and so on.

We can apply any other arithmetic operation in a similar way:

Input
a <- c(22, 10, 7, 3, 14, 4)
b <- c(4, 5, 2, 6, 14, 8)
a / b
Result
 5.5 2.0 3.5 0.5 1.0 0.5

Using the same principle, the first element of the result is 22 / 4 = 5.5, the second is 10 / 5 = 2 and so on.