# Addition
21 + 47
[1] 68
# Subtraction
456 - 564
[1] -108
# Multiplication
7*23
[1] 161
# Division
45/23
[1] 1.956522
John Robin Inston
March 8, 2025
May 30, 2025
This post contains notes for Chapter 2 of my course series Data Science with R covering the basic use of R as a calculator including computations, logic, data types and the assignment operator.
We can perform a wide variety of mathematical calculations using R
including:
The syntax to apply these operations is often very intuitive but slightly different to the mathematical notation. Note that all computations written in R follow the BIDMAS (Brackets, Indices, Division, Multiplication, Addition, Subtraction) order of operations rule and so take care to ensure clarity with parentheses if necessary.
To raise numbers to powers we can use the ^
symbol. Note that if we wish to raise numbers to fractional powers we must ensure to enclose our fraction in parentheses ()
. Further, there is a specifically defined function for computing the square root of a number sqrt()
.
Parentheses ()
, brackets []
and braces {}
all have different uses in R and care should be taken to apply them correctly.
As a quick reminder in mathematics we define the irrational number \(e\) by \[ e = 2.71828... \]
The exponential function \(\exp(\cdot)\) is defined as \[ \exp(x) = e^x. \]
The inverse of this function is known as the natural logarithm \(\log(\cdot)=\ln(\cdot)\) and is defined as the power of which \(e\) will have to be raised to in order to give \(x\). It is therefore the inverse of the exponential function and satisfies \[ \exp(\log(x)) = e^{\log(x)} = x. \]
For a more indepth look at these functions and the results we use to perform calculations with them see my note on Log Laws. To apply these functions in R
we use the exp()
and log()
functions:
[1] 2.718282
[1] 24154953
[1] 3.850148
[1] 47
Verify the following mathematical computations in R
using the operations above:
\(\left(\frac{\log(49^2)}{\exp(4)}\right)^{1/3}=0.5223981\);
\(\left(67+\left(\frac{12}{5} \times \frac{3}{12}\right)\right)^e+\left(\log(21) - \sqrt{14}\right)=94255.7\)
R
can also handle logical inputs including:
<
less than;<=
less than or equal to;>
greater than;>=
greater than or equal to;==
equal to;!=
not equal to;&
AND (\(\cap\) written mathematically);|
OR (\(\cup\) written mathematically).We can write a logical expression and the return will note whether the statement was true or false. For example, the output below is FALSE
because the statement “4 is greater than or equal to 5” is false:
Note that to combine logical expressions we must use &
or |
! For example, trying to evaluate 2 < 4 < 5
will result in an error message.
If we were to save the output of the previous logical expression to an object b
we would see in our environment tab that b
is a boolean type. R
has the capacity to handle multiple data types including:
TRUE
/ FALSE
output of some logical query;''
or ""
.A sequential collection of characters forms a string e.g. "datascience"
but note that this is not a specific datatype in R
unlike other languages such as python.
What will the following logical queries output:
5 > 7
10 <= 10
13 != 12
"Hello" >= "Hell"
"Hello" != "Hell" | 5 < 4
3 < 5 < 7
The assignment operator ->
is used to define objects in R
, i.e. to store them in our short-term memory to use multiple times. Objects can take many different forms from basic numbers to vectors, matrices or even functions, all of which we detail in Chapter 3. For now, suppose we wish to store the results of some of the above computations for future use. We can do so by applying the assignment oeprator:
Running this code you should see the object a
appear in your environment
tab and you might have noticed no result was printed in the console. To print the value of a
in your console you can either use the print()
function or just call the object in your console: