02. Vector Arithmetic and Recycling

Vectors can use simple arithmetic expressions (+, -, *, /) to perform basic operations. Let's first look at addition, then discuss a caveat of vector arithmetics.

Addition and Subtraction

You can add or subtract the corresponding elements of two or more vectors of the same length together.

> c(1,2,3) + c(99,98,97)
[1] 100 100 100
> c(1,2,3) + c(4,5,6)
[1] 5 7 9
> c(1,2,3) - c(1,1,1)
[1] 0 1 2

But what would happen if all the vectors weren't of the same length? Instead of erroring out, R performs recycling.


Recycling occurs when vector arithmetic is performed on multiple vectors of different sizes. R takes the shorter vector and repeats them until it becomes long enough to match the longer one.

> c(1,2,3,4,5,6) + c(1,3)
[1] 2 4 3 7 6 9

As you can see, the c(1,3) vector repeated itself to form c(1,3,1,3,1,3) so that it could successfully match the previous term.

If the shorter vector is not a vector of the longer one, then a warning message appears, but the operation still takes place.

> c(1,2,3,4,5) + c(1,3)
[1] 2 5 4 7 6
Warning message:
In c(1, 2, 3, 4, 5) + c(1, 3) :
  longer object length is not a multiple of shorter object length

Multiplication and Division

Multiplying or dividing vectors is similar to addition and subtraction in that each corresponding element matches up and a product is formed. When the sizes differ, recycling occurs.

> c(1,2,3) * c(0,3,6)
[1] 0 6 18
> c(1,3,5) * c(2,4)
[1] 2 12 10
Warning message:
In c(1, 3, 5) * c(2, 4) :
  longer object length is not a multiple of shorter object length

Operators are mere functions

One small detail to notice is that these common arithmetic expressions are actually functions. Thus, they can be with a similar function notation.

> "*"(5,6)
[1] 30


We can also perform the modulo operator, which outputs the remainder after division of two numbers.

> c(55,54,53) %% c(3)
[1] 1 0 2

Advanced Linear Algebra Operations

You can also apply linear algebra on your vectors in R. To calculate the cross product, use crossprod():

> crossprod(1:3, 4:6)
[1,]    32

You'll notice that the return type isn't a new vector, but instead a one-dimensional matrix. We'll look at matrices in the next lesson.

Become a Bioinformatics Whiz!

Bioinformatics Data Skills

Become a Bioinformatics Whiz! Try Bioinformatics

Learn the best practices used by academic and industry professionals. Bioinformatics Data Skills give a great overview to the Linux Command Line, Github, and other essential tools used in the trade. This book bridges the gap between knowing a few programming languages and being able to utilize the tools to analyze large amounts of biological data.

$ Check price
49.9949.99Amazon 4.5 logo(7+ reviews)

More Bioinformatics resources

Learn to be a Pythonista!

Python Playground

Learn to be a Pythonista! Try Python

Python Playground is a collection of fun programming projects that will inspire you to new heights. You'll manipulate images, build simulations, and interact with hardware using Arduino & Raspberry Pi. With each project, you'll get familiarized with leveraging external libraries for specialized tasks, breaking problems into smaller, solvable pieces, and translating algorithms into code.

$ Check price
29.9529.95Amazon 4 logo(14+ reviews)

More Python resources