-
Notifications
You must be signed in to change notification settings - Fork 0
/
slides.Rmd
210 lines (145 loc) · 4.01 KB
/
slides.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
---
title: "An introduction functions in R"
subtitle: "https://bradduthie.github.com/talks/R_functions.pdf"
author: "Brad Duthie (alexander.duthie@stir.ac.uk)"
date: "06 December 2023"
output:
beamer_presentation: default
header-includes:
- \usepackage{hyperref}
colorlinks: true
linkcolor: blue
urlcolor: blue
---
# What is a function?
\Large
**Functions are ubiquitous and highly useful in R**
\begin{itemize}
\setlength\itemsep{0.4em}
\item Code that is organised to perform a specific task$^{1}$
\item Can get functions in multiple ways
\begin{enumerate} \Large
\item R base functions
\item R package functions
\item Custom functions
\end{enumerate}
\item All functions have a similar structure
\item \textbf{Today you will learn to write functions}
\end{itemize}
\footnotetext[1]{R-Functions. Tutorialspoint. https://www.tutorialspoint.com/r/r\_functions.htm Accessed 6 DEC 2023.}
# What is a function?
```{r, eval = FALSE}
function_name <- function(arg1, arg2 = default){
# Code that can use arguments, arg1 & arg2
return(output);
}
```
\vspace{2mm}
\hrule \pause
\vspace{2mm}
```{r}
my_mean <- mean(x = 1:10);
```
```{r, echo = FALSE}
print(my_mean);
```
\vspace{2mm}
\hrule \pause
\vspace{2mm}
```{r}
class(mean);
```
# Base functions in R
\LARGE
**Base R includes hundreds of functions**
\begin{itemize}
\setlength\itemsep{0.0em}
\item Most \href{https://stat.ethz.ch/R-manual/R-devel/library/base/html/00Index.html}{base functions} not used$^{1}$
\item Familiar functions \texttt{mean}, \texttt{plot}, \texttt{summary}
\item Includes functions like \texttt{+}, \texttt{<-}, \texttt{"}, or \texttt{!}
\end{itemize}
Additional functions can be found in R packages, or custom made and read into the R console.
\footnotetext[1]{https://stat.ethz.ch/R-manual/R-devel/library/base/html/00Index.html}
# Non-base functions in R
**Functions outwith base R available in packages**
\begin{itemize}
\setlength\itemsep{0.0em}
\item \href{https://cran.r-project.org/}{Comprehensive R Archive Network} includes 18000+ packages
\item Packages include specialised functions
\item Access with `install.packages` and `library`
\end{itemize}
```{r, eval = FALSE}
install.packages("ggplot2");
library("ggplot2");
ggplot(data = dat, mapping = aes(x = wgt, y = totlen))
+ geom_point();
```
Custom functions can be written in R too with the `function` function.
# A custom function in R
Convert from Fahrenheit to Celsius
```{r}
F_to_C <- function(F_temp){
C_temp <- (F_temp - 32) * 5/9;
return(C_temp);
}
```
Highlight the whole function and run it, then you can use it.
```{r}
F_to_C(F_temp = 70);
```
Now write a custom function for C to F!
# Functions within functions
**We can use a custom function within another custom function.**
Convert from Fahrenheit to Kelvin.
```{r}
F_to_K <- function(F_temp){
K_temp <- F_to_C(F_temp = F_temp) + 273.15;
return(K_temp);
}
```
Because Kelvin equals degrees Celsius plus 273.15, we can call `F_to_C`, then add 273.15 to it.
# Functions can go in functions
```{r}
F_convert <- function(F_temp = 70,
conversion = "Celsius"){
if(conversion == "Celsius"){
converted <- F_to_C(F_temp = F_temp);
}
if(conversion == "Kelvin"){
converted <- F_to_K(F_temp = F_temp);
}
return(converted);
}
```
Convert to Kelvin again.
```{r}
F_convert(F_temp = 70, conversion = "Kelvin");
```
# Always good to add error messages
\small
```{r}
F_convert <- function(F_temp = 70,
conversion = "Celsius"){
if(conversion != "Celsius" & conversion != "Kelvin"){
stop("'conversion' must be 'Celsius' or 'Kelvin'.");
}
if(is.numeric(F_temp) == FALSE){
stop("F_temp argument must be numeric");
}
if(conversion == "Celsius"){
converted <- F_to_C(F_temp = F_temp);
}else{
converted <- F_to_K(F_temp = F_temp);
}
return(converted);
}
```
# Some additional points
\Huge
\begin{itemize}
\item Recursive functions
\item Function environment
\item Order of arguments
\item Function returns
\item do.call function
\end{itemize}