# Progress Log 106 (R): Vector Arithmetic

- June 2, 2019
- nanakoohashi
- Data Analytics/Data Science, R
- No Comments

#### I’m taking an edX course entitled “Introduction to R for Data Science” and all of the concepts described below come from that course.

Computations are performed **element-wise**.

As you can see, R multiplies each element in the vector with 3.

This works with other mathematical operators as well.

### Element-wise

#### Subtraction

To compute whether your earnings for each day were sufficient to pay for expenses is fairly easy in R. You can simply subtract expenses from earnings to find your daily balance.

This time, the mathematics were also calculated element-wise: the first element of expenses was subtracted from the first element of earnings, the second element of expenses was subtracted from the second element of earnings, and so on.

#### Addition

Similar to subtraction.

#### Multiplication and Division

Multiplication and division in R are different from the traditional matrix and vector operations, where the multiplication of two vectors can result in a single scalar or a matrix.

Multiplication and division are done element-wise!

`sum()`

and `>`

A last question you might ask yourself is how your bank account progressed after the three days.

- Save the previous calculation in a new vector called
`bank`

. - Use the
`sum()`

function on it to calculate the sum of all its elements.

Instead of subtracting expenses from earnings to compare them, which gives you positive and negative numbers, you could also use relational operators to know when earnings were higher than expenses. You can use the greater than `>`

operator for this. This operator compares the numbers in the vectors element-wise. The result is a vector of `TRUE`

s and `FALSE`

s.

On days 1 and 2, your earnings exceeded your expenses, so your corresponding elements are `TRUE`

. On the last day you spent more than you earned so the result is `FALSE`

.