How to Do Basic Arithmetic in R
Since R is statistics platform, it has a rather complete set of arithmetic operators, so you can use R as a fancy calculator if the need arises.
How to Use Basic Operators in R
Most of the basic arithmetic operators are very familiar to programmers (and anybody else who studied math in school).
|x + y||y added to x||2 + 3 = 5|
|x – y||y subtracted from x||8 – 2 = 6|
|x * y||x multiplied by y||3 * 2 = 6|
|x / y||x divided by y||10 / 5 = 2|
|x ^ y (or x ** y)||x raised to the power y||2 ^ 5 = 32|
|x %% y||remainder of x divided by y (x mod y)||7 %% 3 = 1|
|x %/% y||x divided by y but rounded down (integer divide)||7 %/% 3 = 2|
All these operators are vectorized. By using vectorized operators, you can carry out complex calculations with minimal code.
To see how this works, consider these two vectors. One vector represents the number of baskets Granny made during the six games of the basketball season, and the other one represents the number of baskets her friend Geraldine made:
> baskets.of.Granny <- c(12,4,4,6,9,3) > baskets.of.Geraldine <- c(5,3,2,2,12,9)
Suppose that Granny and Geraldine decide to raise money for the Make-A-Wish Foundation and asked people to make a donation for every basket they made. Granny requested $120 per basket, and Geraldine asked for $145 per basket. How do you calculate the total donations that they collected for each game?
R makes the calculation easy. First, calculate how much each lady earned per game, as follows:
> Granny.money <- baskets.of.Granny * 120 > Geraldine.money <- baskets.of.Geraldine * 145
In this example, every value in the vector is multiplied by the amount of money. Check for yourself by taking a look at the values in Granny.money and Geraldine.money.
To get the total money these ladies earned in each game, you simply do this:
> Granny.money + Geraldine.money  2165 915 770 1010 2820 1665
You also could do this in one line, as follows:
> baskets.of.Granny * 120 + baskets.of.Geraldine * 145  2165 915 770 1010 2820 1665
How to control the order of the operations in R
In the previous example, you used both a multiplication and an addition operator. As you see from the result, R correctly multiplies all numbers before adding them together. For all arithmetic operators, the classic rules for the order of operations apply. Calculations are carried out in the following order:
Multiplication and division in the order in which the operators are presented
Addition and subtraction in the order in which the operators are presented
The mod operator (%%) and the integer division operator (%/%) have the same priority as the normal division operator (/) in calculations.
You can change the order of the operations by using parentheses, like this:
> 4 + 2 * 3  10 > (4 + 2)* 3  18
Everything that’s put between parentheses is carried out first.
You also can use basic operators on complex numbers. The complex() function, for example, allows you to construct a whole set of complex numbers based on a vector with real parts and a vector with imaginary parts. For more information, see the Help page for ?complex.