Section 1.3 R as a Calculator
One of the most amazing parts of R is that it can do math for you, and replace that scientific calculator youβre always carrying around. Rβs math range is absolutely incredible, but for right now, we will start with the basics.
Subsection 1.3.1 Basic Math
Speaking all about basics, letβs start off by running some simple math.
2 + 2
10 / 3
5^2
(3 + 7) * 2
5 * 5 * 5 / 37 + 42
[1] 4 [1] 3.333333 [1] 25 [1] 20 [1] 45.37838
Here, each line of code is an individual math equation. These are simple (2 + 2), that with a little working memory, could be done in your head. R can be elevated to perform more mathematical functions that require a little more elbow grease.
Subsection 1.3.2 Built-in mathematical Functions
Building upon
2 + 2, R has built in functions to help get our math homework done. Right now we will do more basic things such as square roots, but in the forthcoming chapters, we will build to more advanced statistical analyses.
# Built-in functions
sqrt(25) # square root
log(10) # natural log
log10(1000) # base-10 log
round(3.14159, digits = 3) # round the output to only have 3 digits
round(5*5/37, 4) # rounding the output of an equation
[1] 5 [1] 2.302585 [1] 3 [1] 3.142 [1] 0.6757
We did a great job of performing a lot of math. As stated before, each line represents a different mathematical equation. While it is a strong start, there will be many times where we want to store our equations or outputs to call later. This is exactly where variables, objects, and vectors come into play.
