forked from alexd106/Rbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.Rmd
More file actions
executable file
·223 lines (159 loc) · 12.6 KB
/
Copy pathindex.Rmd
File metadata and controls
executable file
·223 lines (159 loc) · 12.6 KB
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
---
title: "An Introduction to R"
author: "Alex Douglas, Deon Roos, Francesca Mancini, Ana Couto & David Lusseau"
date: "Draft: `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"
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()
# install the packages needed by this book;
pkgs <- c('vioplot', 'reshape2', 'ggplot2', 'patchwork', 'quantreg', 'hexbin', 'GGally', 'ggfortify', 'kableExtra', 'htmltools', 'rmarkdown', 'xfun', 'knitr')
lapply(pkgs, function(pkg) {
if (system.file(package = pkg) == '') install.packages(pkg)
})
```
```{r create_chunk_options2, include=FALSE, eval=knitr::is_latex_output()}
# source('scripts_and_filters/create_chunk_options.R')
source('wrap_lines.R')
```
# Preface{-}
```{r,echo=F,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. As such, 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 click on the edit button `r icon::fa_edit()` in the navigation bar at the top of this page and issue a pull request. If you want to take a peek at all the code behind the book visit the book GitHub [repo][github-repo]. If you feel like contributing to the book, please get in touch, we'd be happy to hear from you. 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] and [RStudio][rstudio]. 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.
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 [Twitter](#whoarewe) or [GitHub](#whoarewe)).
## 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 Reader in Biostatistics at the University of Aberdeen and has been teaching (and learning) R for the last 15 years.
- Web:https://www.abdn.ac.uk/people/a.douglas/
- Twitter: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 PhD student in applied ecology at the University of Aberdeen researching spatial variation of the temporal kind in population dynamics. He has been using and teaching R for 5 years.
- Web:https://www.abdn.ac.uk/people/deon.roos
- Twitter: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 a PhD student in Ecology at the University of Aberdeen looking at the ecology and physics of tidal stream habitats. She has been using and teaching R for 7 years.
- Web:https://www.abdn.ac.uk/people/r01ac17
- Twitter: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
- Twitter: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 15 years and teaching it for 12 years.
- Twitter:https://twitter.com/lusseau
- GitHub:https://github.com/dlusseau
:::
::::::
## Book website
Although you can use this book as a standalone resource, we recommend you use it in conjunction with the 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 elses, 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 payed back 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 on-line (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 have 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] 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 10 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 enthusuatic contribution of many 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.
\
The R logo is copyright (2016) The R Foundation under Creative Commons Attribution-ShareAlike 4.0 International license (CC-BY-SA 4.0)
```{r links, child="links.md"}
```