Mathematical Functions

R has many built-in mathematical functions. To call a function, we can type its name, followed by open and closing parentheses. Functions take arguments as inputs; anything we type inside the parentheses of a function is considered an argument. The number of arguments can vary from none to multiple, depending on the function. For example:

getwd() #returns an absolute filepath

Doesn't require an argument, whereas, for the next set of mathematical functions, we will need to supply the function a value in order to compute the result.

sin(1)  # trigonometry functions

Output
[1] 0.841471

log(1)  # natural logarithm

Output
[1] 0

log10(10) # base-10 logarithm

Output
[1] 1

exp(0.5) # e^(1/2)

Output
[1] 1.648721


Don't worry about trying to remember every function in R. You can look them up on Google, or if you can remember the start of the function's name, use the tab completion in RStudio.

This is one advantage that RStudio has over R on its own, it has auto-completion abilities that allow you to more easily look up functions, their arguments, and the values that they take.

Typing a ? Before a command's name will open the help page for that command. When using RStudio, this will open the ‘Help' pane; if using R in the terminal, the help page will open in your browser. The help page will include a detailed description of the command and how it works. Scrolling to the bottom of the help page will usually show a collection of code examples that illustrate command usage. We'll go through an example later.