Basic Calculations

In its most basic form, R can be used as like a simple calculator.

Try typing the following into your R console:

5+8
5-8
5*8
5/8
5^8
pi
2*pi
exp(1)
sqrt(100)
100^(0.5)
100^(1/3)
(23+89)/(43-22)

R knows all the usual mathematical functions. The more you use them, the more you’ll remember them. If you ever forget, or need one and you’re not sure, then the internet is your friend!

Here are some examples:

#square root
sqrt(100)
#absolute value
abs(10)
abs(-10)
#trig functions
sin(pi/3)
cos(pi/3)
tan(pi/3)
#R works in radians

#inverse trig functions
asin(0.5)
acos(0.5)
atan(0.5)

#hyperbolic trig functions
sinh(2)
cosh(2)
tanh(2)

#exp and log functions
exp(1)
log(1)     #natural log
log10(10)  #log base 10
log(8,2)

Assigning Variables

In R, the symbol <- means “is assigned”. It’s an arrow which assigns a value to a name of your choosing. We can then “call”" these names later in our code. Here are some simple examples:

x <- 5
x
## [1] 5

This told R that “x is assigned to 5” and then we asked R to tell us what x is.

If we now re-assign x:

x <- 7
x
## [1] 7

the previous value is over-written.

Here are some simple calculations using the named variables x and y:

y <- -3
x+y
## [1] 4
x-y
## [1] 10
x*y
## [1] -21
x/y
## [1] -2.333333

You can also use = to assign values to variables. There’s not much difference between the two symbols in R but there are some technical differences we don’t need to worry about here. If in doubt, use <-.

Now that we have learned about the assignment operator, we can avoid repeating code unnecessarily.

For example, suppose we need to use the following values

\[\begin{align*} a &= 2 \\ b &= 3 \\ c &= -9 \end{align*}\]

in the evaluation of:

\[ \frac{\sqrt{2a - 3c}}{a^b}. \]

We could code this as a one time calculation:

sqrt((2*2)-(3*-9))/(2^3)
## [1] 0.6959705

but it is usually better to use assignment as in the following:

a <- 2
b <- 3
c <- -9

sqrt((2*a)-(3*c))/(a^b)
## [1] 0.6959705

This way, we can easily re-use the same formula with different inputs:

a <- 4
b <- 2
c <- -3

sqrt((2*a)-(3*c))/(a^b)
## [1] 0.2576941

We can also assign the output of the formula to a new variable name which we can call later in the code. Here’s a simple example:

a <- 4
b <- 2
c <- -3

d <- sqrt((2*a)-(3*c))/(a^b)

d + 10
## [1] 10.25769

It is often a good idea to split a formula up and evaluate it in parts. These parts can then be put together to obtain the final result. Although the formula above is not very complex, we will use it to illustrate the general idea:

a <- 4
b <- 2
c <- -3

S <- (2*a)-(3*c)
N <- sqrt(S)
D <- a^b

d <- N/D

d + 10
## [1] 10.25769

Naming Variables

It is always a good idea to give your variables appropriate and memorable names. For example, calling the output of the formula above d is not especially helpful. It might be better to name it something like formula.out:

a <- 4
b <- 2
c <- -3

formula.out <- sqrt((2*a)-(3*c))/(a^b)

formula.out + 10
## [1] 10.25769

This will help you to read over your code and to understand what is going on especially in more complex programming problems.

Additionally, R does not like you to name objects with a number first. So, for example, 1.output would not be accepted. Try output.1 instead.