---
date: "2021-01-01"
title: "17. Functions by Chapter"
---
```{r code-brewing-opts, echo=FALSE}
knitr::opts_chunk$set(
comment = "R>",
warning = FALSE,
message = FALSE,
fig.width = 4.5,
fig.height = 2.625,
out.width = "75%",
fig.asp = NULL, # control via width/height
dpi = 300
)
ggplot2::theme_set(
ggplot2::theme_minimal(base_size = 8)
)
ggplot2::theme_set(
ggplot2::theme_bw(base_size = 8)
)
```
## Useful Information
...incomplete...
### Operators
There are several operators you can use to help build expressions as shown in Table \ref{tab:operators}.
\begin{table}[htbp]
\begin{small}
\begin{center}
\caption{Logical operators for use in R.}
\label{tab:operators}
\newcolumntype{R}{>{\raggedright\arraybackslash}X}%
\begin{tabularx}{\textwidth}{l X l}
\toprule
Operator & Meaning & Example \\
\midrule
\texttt{\textless{}} & less than & \texttt{x\ \textless{}\ 3} \\
\texttt{\textgreater{}} & greater than & \texttt{x\ \textgreater{}\ 5} \\
\texttt{\textgreater{}=} & greater than or equal to & \texttt{x\ \textgreater{}=\ 24} \\
\texttt{\textless{}=} & less than or equal to & \texttt{x\ \textless{}=\ 19} \\
\texttt{==} & exactly equal to & \texttt{x\ ==\ 666} \\
\texttt{!=} & not equal to & \texttt{x\ !=\ 777} \\
\texttt{\&} & AND; returns TRUE if statements on both sides of the \texttt{\&} are TRUE & \texttt{(x\ \textgreater{}\ 3)\ \&\ (y\ !=\ 5)} \\
\texttt{\textbar{}} & OR; the pipe symbol \texttt{\textbar{}}; TRUE if a statement on either side of the \texttt{\textbar{}} is TRUE & \texttt{(x\ \textless{}\ 10)\ \textbar{}\ (x\ \textgreater{}\ 20)} \\
\bottomrule
\end{tabularx}
\end{center}
\end{small}
\end{table}
\begin{mybox}{}{Question}
The logical AND: If we had said \texttt{subset(dat, site == "Kommetjie" \& site == "Baboon")}, what would have happened? Why? Try it and see...
\end{mybox}
### Functions
Some example functions covered so far are presented in Table \ref{tab:functions}.
\begin{table}[htbp]
\begin{small}
\begin{center}
\caption{Common functions used for daily work in R.}
\label{tab:functions}
\newcolumntype{R}{>{\raggedright\arraybackslash}X}%
\begin{tabularx}{\textwidth}{R l R R }
\toprule
\textbf{Type} & \textbf{Function} & \textbf{What it does} & \textbf{Syntax} \\
\midrule
Checking a dataframe & \texttt{class()} & Gives the class (type of data structure) of the object & \texttt{class(dat)} \\
Numerical calculations & \texttt{mean()}, \texttt{sd()} & Calculations on a numerical R object (variables, vectors, arrays) & \texttt{mean(dat\$var)} \\
Gets working directory & \texttt{getwd()} & Gets current working directory & \texttt{getwd()} \\
Sets working directory & \texttt{setwd()} & Sets working directory to specified path & \texttt{setwd("yourpath")} \\
Getting your data into R & \texttt{read.table()} & Reads file in table format (with specified separator sep) and creates a dataframe from it & \texttt{read.table("filename.csv", header= TRUE, sep = ",")} \\
Getting .csv file into R & \texttt{read.csv()} & Reads in .csv file to a dataframe; as above, but `sep' predefined & \texttt{read.csv("filename.csv", header= TRUE)} \\
List variables & \texttt{ls()} & Lists objects in memory & \texttt{ls()} \\
Variable names in dataframe & \texttt{names()} & Gives variable names & \texttt{names(dat)} \\
Subsetting a dataframe & \texttt{subset()} & Subset a dataframe & \texttt{subset(dat, Var1 == "Low" | Var2 <= 5)} \\
Cleaning up & \texttt{rm()} & Removes object(s) from the Environment & \texttt{rm(object\_name)}; \texttt{rm(list = ls())} \\
\bottomrule
\end{tabularx}
\end{center}
\end{small}
\end{table}
Some summary functions are presented in Table \ref{tab:summaries}.
\begin{table}[htbp]
\begin{small}
\begin{center}
\caption{Commonly used and useful summary functions.}
\label{tab:summaries}
\newcolumntype{R}{>{\raggedright\arraybackslash}X}%
\begin{tabularx}{\textwidth}{R R R X }
\toprule
\textbf{Type} & \textbf{Function} & \textbf{What it does} & \textbf{Syntax} \\
\midrule
Specifying variable in dataframe & \texttt{\$} & Gives the class (type of data structure) of the object & \texttt{dat\$blade\_length} \\
& \texttt{str()} & Lists variables in your object by name, their data type (continuous, factor), an indication of the actual data & \texttt{str(dat)} \\
& \texttt{head()} & Gives first 6 rows of a dataframe, or number of rows (\texttt{n}) specified & \texttt{head(dat, 20)} \\
& \texttt{tail()} & Gives last 6 (or \texttt{n}) rows of a dataframe & \texttt{tail(dat, 10)} \\
& \texttt{names()} & Gives variable names & \texttt{names(dat)} \\
& \texttt{summary()} & Tabulates variables in dataframe and provides summary statistics & \texttt{summary(dat)} \\
<!-- & \texttt{attach()} & Loads dataframe into memory & \texttt{attach(dat)} \\ -->
<!-- & \texttt{detach()} & Removes dataframe from memory & \texttt{detach(dat)} \\ -->
& \texttt{with()} & Specify a dataframe & \texttt{with(dat, mean(blade\_length))} \\
Summary statistics & \texttt{mean()}, \texttt{sd()}, \texttt{range()}, \texttt{var()} & Calculates particular statistic for a variable. Use \texttt{na.rm = TRUE} if your data contain \texttt{NA}s & \emph{e.g.} \texttt{mean(dat\$blade\_length, na.rm = TRUE)} \\
& \texttt{length()} & Number of elements in a variable. Use \texttt{na.omit()} if your data contain \texttt{NA}s & \texttt{length(dat\$blade\_length)}; \texttt{length(na.omit(dat\$blade\_length))} \\
Data manipulation & \texttt{table()} & Frequency table by variables specified & \texttt{table(dat\$site, dat\$blade\_length)} \\
& \texttt{ftable()} & Flat frequency table useful for displaying multidimensional tables & \texttt{ftable(dat\$catvar1, dat\$catvar2, dat\$catvar3)} \\
Saving an object file & \texttt{write.csv()} & Save an object to a .csv file & \texttt{write.csv(sp\_by\_site, file = "Laminaria.csv", row.names = TRUE)} \\
\bottomrule
\end{tabularx}
\end{center}
\end{small}
\end{table}