An Introduction R
Preface
0.1
The aim of this book
0.2
Who is this book for?
0.3
Why an open book?
0.4
Who are we?
0.5
How to use this book
0.6
Book website
0.7
Some R pointers
0.8
Thanks
0.9
License
1
Getting started with R and RStudio
1.1
Installing R
1.1.1
Windows users
1.1.2
Mac users
1.1.3
Linux users
1.1.4
Testing R
1.2
Installing RStudio
1.2.1
Windows and Mac users
1.2.2
Linux users
1.2.3
Testing RStudio
1.3
RStudio orientation
1.3.1
Console
1.3.2
Environment/History/Connections
1.3.3
Files/Plots/Packages/Help/Viewer
1.4
Alternatives to RStudio
1.4.1
Advanced text editors
1.4.2
Integrated development environments
1.5
R packages
1.5.1
CRAN packages
1.5.2
Bioconductor packages
1.5.3
GitHub packages
1.5.4
Using packages
1.6
Projects in RStudio
1.7
Working directories
1.8
Directory structure
1.9
File names
1.10
Project documentation
1.11
R style guide
1.12
Backing up projects
1.13
Citing R
1.14
Exercise 1
2
Some R basics
2.1
Getting started
2.2
Objects in R
2.2.1
Creating objects
2.2.2
Naming objects
2.3
Using functions in R
2.4
Working with vectors
2.4.1
Extracting elements
2.4.2
Replacing elements
2.4.3
Ordering elements
2.4.4
Vectorisation
2.4.5
Missing data
2.5
Getting help
2.5.1
R help
2.5.2
Other sources of help
2.6
Saving stuff in R
2.7
Exercise 2
3
Data in R
3.1
Data types
3.2
Data structures
3.2.1
Scalars and vectors
3.2.2
Matrices and arrays
3.2.3
Lists
3.2.4
Data frames
3.3
Importing data
3.3.1
Saving files to import
3.3.2
Import functions
3.3.3
Common import frustrations
3.3.4
Other import options
3.4
Wrangling data frames
3.4.1
Positional indexes
3.4.2
Logical indexes
3.4.3
Ordering data frames
3.4.4
Adding columns and rows
3.4.5
Merging data frames
3.4.6
Reshaping data frames
3.5
Summarising data frames
3.6
Exporting data
3.6.1
Export functions
3.6.2
Other export functions
3.7
Exercise 3
4
Graphics with base R
4.1
Getting started
4.2
Simple base R plots
4.2.1
Scatterplots
4.2.2
Histograms
4.2.3
Box and violin plots
4.2.4
Dot charts
4.2.5
Pairs plots
4.2.6
Coplots
4.2.7
Lattice plots
4.3
Customising plots
4.3.1
Customising with arguments
4.3.2
Building plots
4.4
Multiple graphs
4.5
Exporting plots
4.6
Exercise 4
5
Graphics with ggplot
5.1
Beginning at the end
5.2
The start of the end
5.2.1
The purest of ggplots
5.2.2
Wrapping grids
5.2.3
Plotting multiple ggplots
5.2.4
Make it your own
5.2.5
Setting the theme
5.2.6
Prettification
5.3
Tips and tricks
5.3.1
Statistics layer
5.3.2
Axis limits and zooms
5.3.3
Layering layers
5.3.4
Continuous colours
5.3.5
Size of points
5.3.6
Moving the legend
5.3.7
Hiding the legend
5.3.8
Hiding part of the legend
5.3.9
Writing on a figure
5.3.10
Axes tick marks and tick labels
5.3.11
More advanced patchwork
5.4
A ggplot bestiary
5.4.1
Density plot
5.4.2
Histogram
5.4.3
Frequency polygons
5.4.4
Boxplot
5.4.5
Violin plots
5.4.6
Barchart
5.4.7
Quantile lines
5.4.8
Heatmap
5.4.9
Hex map
5.4.10
Contour map
5.4.11
Cleveland dotplot
5.4.12
Pairs plot
5.5
Exercise 5
6
Simple Statistics in R
6.1
One and two sample tests
6.2
Correlation
6.3
Simple linear modelling
6.4
Other modelling approaches
6.5
Exercise 6
7
Programming in R
7.1
Looking behind the curtain
7.2
Functions in R
7.3
Conditional statements
7.4
Combining logical operators
7.5
Loops
7.5.1
For loop
7.5.2
While loop
7.5.3
When to use a loop?
7.5.4
If not loops, then what?
7.6
Exercise 7
8
Reproducible reports with R markdown
8.1
What is R markdown?
8.2
Why use R markdown?
8.3
Get started with R markdown
8.4
Create an R markdown document
8.5
R markdown anatomy
8.5.1
YAML header
8.5.2
Formatted text
8.5.3
Code chunks
8.5.4
Adding figures
8.5.5
Adding tables
8.5.6
Inline R code
8.6
Some tips and tricks
8.7
Further Information
9
Version control with Git and GitHub
9.1
What is version control?
9.2
Why use version control?
9.3
What is Git and GitHub?
9.4
Getting started
9.4.1
Install Git
9.4.2
Configure Git
9.4.3
Configure RStudio
9.4.4
Register a GitHub account
9.5
Setting up a project in RStudio
9.5.1
Option 1 - GitHub first
9.5.2
Option 2 - RStudio first
9.6
Using Git
9.6.1
Tracking changes
9.6.2
Commit history
9.6.3
Reverting changes
9.6.4
Collaborate with Git
9.6.5
Git tips
9.7
Further resources
Appendix
A
Installing R Markdown
A.1
MS Windows
A.2
Mac OSX
Published with bookdown
An Introduction to R
An Introduction to R
Alex Douglas, Deon Roos, Francesca Mancini, Ana Couto & David Lusseau
April 9, 2024
Preface