This is the first in a planned series of notes intended as supplementary materials for participants in the course on Advanced Quantitative Methods that I currently teach in the Graduate School for Social Sciences (GSSR) in Warsaw. Any comments about the contents of this note are welcome.

Using R for calculations

You can use R to make calculations — both simple and complex.

  1. Addition
2 + 3
## [1] 5
  1. Subtraction
2 - 3
## [1] -1
  1. Multiplication
2 * 3
## [1] 6
  1. Division
2/3
## [1] 0.6666667
  1. Power. You can use ^ or **. Personally, I prefer the former, as it requires less typing.
2 ^ 3
## [1] 8
2 ** 3
## [1] 8
  1. Square roots
sqrt(9)
## [1] 3
  1. Other roots. For instance, \(\sqrt[3]{8} = 8^{1/3} = 2\):
8 ^ (1/3)
## [1] 2
  1. Absolute values (ignoring sign):
abs(-3)
## [1] 3
  1. Integer division: division in which the fractional part is discarded. For instance, 5/2 = 2.5, but in the integer division we discard the fractional part, so that the result is 2.
5 %/% 2
## [1] 2
  1. Remainder from divison. For instance, in the previous example we divided 5 by 2. Well, to “fits in” 5 twice, leaving a remainder of 1.
5 %% 2
## [1] 1

Hierarchy of arithmetic operations

In R, mathematical operations are performed in the right order. For instance, \(2 + 2\cdot 2 = 6\), because multiplication has precendence over addition. This also applies to division and subtraction, so that \(2 - 2/2 = 1\). However, \((2 + 2) \cdot 2 = 8\), as the operation within the parentheses has precendence. In turn, powers have precedence over multiplication:

4^2 - 3*2
## [1] 10

Assigning names to objects

If you want to save a result for further calculations, it’s often useful to assign it a name and refer to the name — rather than the original mathematical expression — in subsequent operations.1 Remember to use <- for the assignment, not =. For instance, suppose we have the following two operations:

4 ^ 2 - 3 * 2
## [1] 10
4 ^ (2 - 3) * 2
## [1] 0.5

Suppose further that we now want to subtract the latter result from the former. Without the assignments, we’d have to write the following:

(4 ^ 2 - 3 * 2) - (4 ^ (2 - 3) * 2)
## [1] 9.5

This is correct, of course, but that is a lot of typing and you have remember about the extra parentheses to separate one expression from the other. It would be much simpler to assign names to the operations:

a_1 <- 4 ^ 2 - 3 * 2
a_2 <- 4 ^ (2 - 3) * 2

Typing a name in the console returns its value:

a_1
## [1] 10
a_2
## [1] 0.5

Always keep in mind that case matters: A_1 is not the same thing as a_1:

A_1
## Error in eval(expr, envir, enclos): object 'A_1' not found

Typos also matter: a.1 is not the same thing as a_1:

a.1
## Error in eval(expr, envir, enclos): object 'a.1' not found

Then, you can use the names in further calculations:

a_1 - a_2
## [1] 9.5

R now understands that a_1 is simply a shorthand for 4 ^ 2 - 3 * 2, whereas a_2 is a shorthand for 4 ^ (2 - 3) * 2.

You can also create new objects using the existing ones:

a_3 <- a_1 - a_2
a_4 <- a_1 + a_2

and go on to perform still further calculations using the newly created objects:

a_3/a_4
## [1] 0.9047619

  1. Check out this brief section for a little overview about name assignments in R.