-
Notifications
You must be signed in to change notification settings - Fork 46
/
index.Rmd
executable file
·286 lines (198 loc) · 15.8 KB
/
index.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
---
title: "An Introduction to R"
author: "Alex Douglas, Deon Roos, Francesca Mancini, Ana Couto & David Lusseau"
date: "`r gsub(' 0', ' ', format(Sys.Date(), '%B %d, %Y'))`"
site: bookdown::bookdown_site
output: bookdown::gitbook
always_allow_html: true
documentclass: book
#bibliography: [book.bib, packages.bib]
#biblio-style: apalike
link-citations: yes
colorlinks: yes
geometry: "left=3cm,right=3cm,top=2cm,bottom=2cm"
fontsize: 12pt
monofont: "Source Code Pro"
monofontoptions: "Scale=0.7"
# linestretch: 1.15
github-repo: "alexd106/Rbook"
description: "A practical introduction to using R for data analysis"
url: 'https\://alexd106.github.io/Rbook/'
cover-image: "images/cover_v3.png"
---
```{r create_chunk_options1, include=FALSE}
source('utils.R')
newest.vers <- checkRversion()
```
```{r create_chunk_options2, include=FALSE, eval=knitr::is_latex_output()}
# source('scripts_and_filters/create_chunk_options.R')
source('wrap_lines.R')
```
# Preface{-}
\markboth{PREFACE}{}
```{r,echo=FALSE,fig.align="center",eval=knitr::is_html_output(), out.width='50%'}
knitr::include_graphics('images/cover_v3.png')
```
----
## The aim of this book
The aim of this book is to introduce you to using R, a powerful and flexible interactive environment for statistical computing and research. R in itself is not difficult to learn, but as with learning any new language (spoken or computer) the initial learning curve can be a little steep and somewhat daunting. We have tried to simplify the content of this book as much as possible and have based it on our own personal experience of teaching (and learning) R over the last 15 years. It is not intended to cover everything there is to know about R - that would be an impossible task. Neither is it intended to be an introductory statistics course, although you will be using some simple statistics to highlight some of R’s capabilities. The main aim of this book is to help you climb the initial learning curve and provide you with the basic skills and experience (and confidence!) to enable you to further your experience in using R.
## Who is this book for?
We hope this book will be a useful introduction for anyone who wants to learn how to use R. It started out as a 100 page pdf manual used to support our teaching in numerous undergraduate and postgraduate biostatistics classes at the [University of Aberdeen][uoa] and has evolved over the last 10 years to the book you're now reading. Consequently, this book is primarily written with the advanced undergraduate or postgraduate student in mind although the material should be useful for anyone with an interest in learning R regardless of background. Our book assumes that you have no previous experience of using either R or RStudio and no background in programming or using command-line driven software. We've also tried to make the content of this book operating system agnostic and have included information for Windows, Mac and Linux users where appropriate.
## Why an open book?
We decided to write this book as a freely available open source document for a number of reasons. Firstly, we hope this book will be a living document that naturally evolves over time. If you find any errors in the book or want to make a suggestion on how to improve it, please open an issue on the GitHub [site][github-issue] or even better click on the edit button `r icons::fontawesome('edit', style = 'solid')` in the navigation bar at the top of this page and issue a pull request (only available on the web version of this book). Please note that this book uses a [Contributor Code of Conduct][code-conduct]. By contributing to this book, you agree to abide by its terms. If you feel like contributing to the book, please get in touch, we'd be happy to hear from you. If you want to take a peek at all the code behind the book visit the book GitHub [repo][github-repo]. Secondly, the release of the [bookdown][bookdown] package by Yihui Xie has made it incredibly easy to collaboratively write, update and maintain this book using [RMarkdown][rmarkdown], [RStudio][rstudio] and [GitHub][github]. Without the bookdown package this book wouldn't exist in its current format. Lastly, R is not just software, it's also a wonderful open community with many thousands of contributors all working to make R even easier to learn and use. This is our small contribution.
## Who are we? {#whoarewe}
:::::: {.columns}
::: {.column width="20%" data-latex="{0.20\textwidth}"}
```{r, echo=FALSE, out.width="90%", fig.align="center"}
knitr::include_graphics(path = "images/alex.png")
```
:::
::: {.column width="5%" data-latex="{0.05\textwidth}"}
\
<!-- an empty Div (with a white space), serving as
a column separator -->
:::
::: {.column width="75%" data-latex="{0.75\textwidth}"}
**Alex Douglas** is a Professor of Biostatistics at the University of Aberdeen and has been teaching (and learning) R since version 2.1.
- Web:https://www.abdn.ac.uk/people/a.douglas/
- X:https://twitter.com/Scedacity
- GitHub:https://github.com/alexd106
:::
::::::
\
:::::: {.columns}
::: {.column width="20%" data-latex="{0.20\textwidth}"}
```{r, echo=FALSE, out.width="90%", fig.align="center"}
knitr::include_graphics(path = "images/deon.png")
```
:::
::: {.column width="5%" data-latex="{0.05\textwidth}"}
\
<!-- an empty Div (with a white space), serving as
a column separator -->
:::
::: {.column width="77%" data-latex="{0.75\textwidth}"}
**Deon Roos** is a Lecturer in Applied Statistics at the University of Aberdeen. He's been using and teaching R for 10 years.
- X:https://twitter.com/DeonRoos88
- GitHub:https://github.com/Deon88
:::
::::::
\
:::::: {.columns}
::: {.column width="20%" data-latex="{0.20\textwidth}"}
```{r, echo=FALSE, out.width="90%", fig.align="center"}
knitr::include_graphics(path = "images/ana.png")
```
:::
::: {.column width="5%" data-latex="{0.05\textwidth}"}
\
<!-- an empty Div (with a white space), serving as
a column separator -->
:::
::: {.column width="75%" data-latex="{0.75\textwidth}"}
**Ana Couto** is an ecological statistician. She has been using and teaching R for 9 years.
- Web:https://t.co/KRUlw1Grjr
- X:https://twitter.com/AnaSofiaCouto
- GitHub:https://github.com/Anofia
:::
::::::
\
:::::: {.columns}
::: {.column width="20%" data-latex="{0.20\textwidth}"}
```{r, echo=FALSE, out.width="90%", fig.align="center"}
knitr::include_graphics(path = "images/francesca2.png")
```
:::
::: {.column width="5%" data-latex="{0.05\textwidth}"}
\
<!-- an empty Div (with a white space), serving as
a column separator -->
:::
::: {.column width="75%" data-latex="{0.75\textwidth}"}
**Francesca Mancini** is an ecological modeller, working with large citizen science datasets and statistical models to derive trends in biodiversity change and investigate its drivers.
- Web:https://FrancescaMancini.github.io
- X:https://twitter.com/frances_mancini
- GitHub:https://github.com/FrancescaMancini
:::
::::::
\
:::::: {.columns}
::: {.column width="20%" data-latex="{0.20\textwidth}"}
```{r, echo=FALSE, out.width="90%", fig.align="center"}
knitr::include_graphics(path = "images/lusseau.png")
```
:::
::: {.column width="5%" data-latex="{0.05\textwidth}"}
\
<!-- an empty Div (with a white space), serving as
a column separator -->
:::
::: {.column width="75%" data-latex="{0.75\textwidth}"}
**David Lusseau** is a Professor of Marine Sustainability at the Danish Technical University. He's been using R for 17 years and teaching it for 14 years.
- X:https://twitter.com/lusseau
- GitHub:https://github.com/dlusseau
:::
::::::
## How to use this book
For the best experience we recommend that you read the web version of this book. The web version includes a navbar at the top of the page where you can toggle the sidebar on and off `r icons::fontawesome('bars', style = 'solid')`, search through the book `r icons::fontawesome('search', style = 'solid')`, change the font, font size and page colour `r icons::fontawesome('font', style = 'solid')` and suggest revisions if you spot a typo or mistake `r icons::fontawesome('edit', style = 'solid')`. You can also download a pdf version of our book by clicking on the pdf icon `r icons::fontawesome('file-pdf', style = 'solid')`.
We use a few typographical conventions throughout this book.
R code and the resulting output are presented in code blocks in our book.
```{r, echo=TRUE, eval=TRUE, collapse=TRUE}
42 + 1
```
If you're running R code in the R console it will look a little different from the code above. Code should be entered after the command prompt (`>`) and the output from code in the console will not be commented with `##`.
```{r, echo=TRUE, collapse=TRUE, prompt=TRUE, comment=""}
42 + 1
```
In the book text we refer to R functions using code font followed by a set of round brackets, i.e. `mean()` or `sd()` etc.
We refer to objects that we create using code font without the round brackets, i.e. `obj1`, `obj2` etc.
A series of actions required to access menu commands in RStudio are identified as `File` -> `New File` -> `R Script` which translates to 'click on the File menu, then click on New File and then select R Script'.
Clickable links to sections in the book or to external websites are highlighted in light blue text, i.e. [link][cran].
Links to short 'how-to' videos that accompany this book are indicated by a video `r icons::icon_style(icons::fontawesome(name = 'video', style = 'solid'), fill = "#165788", scale = 1.3)` icon.
Links to exercises can be found at the end of each Chapter and are highlighted by a info `r icons::icon_style(icons::fontawesome(name = 'info-circle', style = 'solid'), fill = "#416dde", scale = 1.3)` icon.
## Book website
Although you can use this book as a standalone resource, we recommend you use it in conjunction with it's companion [website][course-web]. The course website contains a series of exercises which will help you practice writing R code and test your understanding of key concepts - you certainly won’t learn how to use R by watching other people do it (or reading a book about it!). The website also contains solutions for each of the exercises and a plethora of links to additional tutorials and resources.
## Some R pointers
A question we get asked fairly regularly is *'what's the best/easiest way to learn R?'*. Unfortunately, we don't have a ready answer to this question as everyone tends to learn R in their own way and at their own pace. Having said that, here are a few things to bear in mind that might help:
- Use R often and use it regularly - find any excuse to fire up RStudio (or just R) and get coding. This will help build and maintain all important momentum.
- Learning R is not a memory test. One of the beauties of a scripting language is that you will always have your code to refer back to when you inevitably forget how to do something.
- You don't need to know everything there is to know about R to use it productively. If you get stuck, Google it, it's not cheating and writing a good search query is a skill in itself. Just make sure you check thoroughly that the code you find is doing what you want it to do.
- If you find yourself staring at code for hours trying to figure out why it's not working then walk away for a few minutes. We've lost count of the number of times we were able to spot our mistake almost immediately after returning from a short caffeine break.
- In R there are many ways to tackle a particular problem. If your code doesn't look like someone else's, but it does what you want it to do, in a reasonable time and robustly then don't worry about it - job done.
- Related to our previous point, remember R is just a tool to help you answer your interesting questions. Although it can be fun to immerse yourself in all things R (we often do), don't lose sight of what's important - your research question(s) and your data. No amount of skill using R will help if your data collection is fundamentally flawed or your question vague.
- Recognise that there will be times when things will get a little tough or frustrating. Try to accept these periods as part of the natural process of learning a new skill (we've all been there) and remember, the time and energy you invest now will be more than recouped in the not too distant future.
Finally, once you've finished working your way through this book, we encourage you to practice what you've learned using your own data. If you don’t have any data yet, then ask your colleagues / friends / family for some (we're sure they will be delighted!) or follow one of the many excellent tutorials available online (see the course [website][course-web] for more details). Our suggestion to you, is that while you are getting to grips with R, uninstall any other statistics software you have on your computer and only use R. This may seem a little extreme but will hopefully remove the temptation to *‘just do it quickly’* in a more familiar environment and consequently slow down your learning of R. Believe us, anything you can do in your existing statistics software package you can do in R - often more robustly and efficiently.
Good luck and don’t forget to have fun.
## Thanks
The vast majority of the content of this book has been used to help teach our applied statistics courses in the [School of Biological Sciences][sbs] and [School of Medicine, Medical Sciences and Nutrition][smmsn] at the [University of Aberdeen][uoa]. It's been tremendous fun to meet and interact with the 1000's of students who have attended these courses over the last 15 years, many of whom helped improve this book by suggesting changes and spotting our daft mistakes (we still own any that remain!). Teaching these courses would have been much more difficult (and a lot less fun) without the enthusiastic help of a small army of postgraduate instructors. Their skill, patience and passion for teaching R and statistics has been a constant source of inspiration over the years. Thank you all (you know who you are!).
Thanks also to the following who have generously contributed fixes and improvements to the book via pull requests, raising an issue or email:
- Frédéric Santos ([\@frederic-santos](https://github.com/frederic-santos))
- azzco ([\@azzco](https://github.com/azzco))
- Cian McAuley
- Constantinos C ([\@Gesler16](https://github.com/Gesler16))
- Lee Sanders ([\@LSanders1](https://github.com/LSanders1))
- Dynamic6844 ([\@Dynamic6844](https://github.com/Dynamic6844))
- Antonio Castellano Albors
- Nazmul Hasan ([\@nazmulhasan202](https://github.com/nazmulhasan202))
- Metehan Gungor ([\@gungorMetehan](https://github.com/gungorMetehan))
- Irvine Tatenda Mutiti ([\@it1mutiti](https://github.com/it1mutiti))
- Katarzyna Zejc
## License
We want you to be able to enjoy this book completely free of charge so we've licensed it according to the Creative Commons Attribution-NonCommercial 4.0 International (CC BY-NC 4.0) License (https://creativecommons.org/licenses/by-nc/4.0/).
If you teach R, feel free to use some or all of the content in this book to help support your own students. The only thing we ask is that you acknowledge the original source and authors. If you find this book useful or have any comments or suggestions we would love to hear from you (you'll find us lurking on [X](#whoarewe) and [GitHub](#whoarewe)).
\
*This is a human-readable summary of (and not a substitute for) the license. Please see <https://creativecommons.org/licenses/by-nc/4.0/legalcode> for the full legal text.*
**You are free to:**
- **Share**---copy and redistribute the material in any medium or
format
- **Adapt**---remix, transform, and build upon the material
The licensor cannot revoke these freedoms as long as you follow the
license terms.
**Under the following terms:**
- **Attribution**---You must give appropriate credit, provide a link
to the license, and indicate if changes were made. You may do so in
any reasonable manner, but not in any way that suggests the licensor
endorses you or your use.
- **NonCommercial**---You may not use the material for commercial purposes.
```{r links, child="links.md"}
```