# Glossary of R terms and characters

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
This document is meant as a quick reference guide to some of the terms and special characters used in `R`. It is not a guide to the functions used throughout the course.  

R is freely available and can be downloaded from https://cran.r-project.org/. There are versions for Windows and Mac users. 

## Terminology

\textbf{arguments/default values} - parameters that need to be specified by the user in order to execute a function. Some arguments may have pre-specified, or default, values and so don't need to be changed unless different options are required. 

\textbf{function} - collection of `R` commands to perform a particular task. Can be in-built (e.g. `mean`) or written by the user.

\textbf{package} - collection of functions.

\textbf{Rstudio} - graphical user interface to `R`.

\textbf{working directory} - directory specified by user and subsequently used, for example, to read files from or to save files to. 

\textbf{workspace} - file containing data objects created during R session. Has filename suffix '.RData' by default, but this can be changed by user.

## Useful commands

`setwd` function - sets the working directory (i.e. `setwd("C:/mt1007/R")`). 

`getwd()` - returns the working directory

`ls()` - lists objects in the R workspace

`save.image()` - saves everything in current workspace to '.RData' file

`load(".RData")` - loads workspace

`help` function - provides details about the specified function and what arguments it requires (i.e. `help(sample)`)

## Useful menu items in RStudio

Some of the basic commands in R used to navigate your directories can be performed using the menu items in RStudio. A few are listed below. 

\textbf{File > Open file} - opens file (this could be an `.rmd' file or a text file containing R commands)

\textbf{File > Save} - saves the active file. 

\textbf{Session > Set working directory > Choose directory} - sets the working directory

\textbf{Session > Load workspace} - loads the chosen `.RData` file

## Special characters

`+`,`-`,`*`,`/` arithmetric operators

`:` as in `a:b` produces a vector from `a` to `b`, inclusive (e.g. `5:10` produces a vector 5 6 7 8 9 10)

`<-`  assignment symbol (e.g. `x <- 5:10`)

`[]`  used for selection (e.g. `x[1]` selects first element of a vector `x`) 

`[rows, columns]` - used for selection of a dataframe where `rows` is the criteria for selecting rows and `columns` is the criteria for selecting the columns

`&` intersection, 'and' 

`|` or

`!` not 

`<` less than

`<=` less than or equal to

`>` greater than

`>=` greater than or equal to

`==` logical equals, used in selection to compare two values

`!=` not equal to 

`=` used to specify values for function arguments (e.g. `sample(x=1:30, size=5)`)

`~` notation for specifing a model as in `y ~ x` 

`^` to the power of (e.g. `x^2` is $x^2$)

`#` comment; any code/text after `#` on the same line is ignored 

`$` used to define elements (columns/object) within an R object (e.g. data$X)

`NA` represents missing values 

`%` used for matrix multiplication (e.g. `x %*% y`)

`%>%` pipe used in `tidyverse` to mean 'take the object before the pipe and feed into the next step

`pi` is $\pi$ i.e. 3.141593

## Useful hints

uparrow key - recalls previous command in Console panel 

R is case specific and so `A` and `a` are treated as separate objects   

If a package is not listed on the 'Packages' tab, it can be installed from the CRAN Repository

