All complex analyses comprise sequences of multiple simple operations. To get acquainted with R and "build trust," please read about these operations you can implement in the R (RStudio) console and immediately see the results. Run the codes on your computer. Do you get the same outputs? Try changing some of the code to see how the outputs will change. You should be able to guess what kind of output you can get from any of these simple operations.
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 |
---|
|
log(1) # natural logarithm
Output |
---|
|
log10(10) # base-10 logarithm
Output |
---|
|
exp(0.5) # e^(1/2)
Output |
---|
|
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.