Chapter 2 Setting up
Unfortunately, as with any new skill, the first chapter can be the most text heavy. Apologies. We will do our best to;
- Keep it as brief as possible
- clearly structured so you can skip sections as needed
- complete so you know that after this chapter you will be focused on coding.
2.1 About R and RStudio
R
is a progamming language that is primarily focused on statistics. This means that it’s a way of talking to your computer to get it to solve certain problems and R
’s speciality is statistics and data analysis.
It’s not all it can do and is a great pathway to transition from point-and-click data analysis to more general programming. The skills you will learn along the way are part of most modern languages, so your time will be well spent!
There are many reasons individuals use R
but for our needs the main selling points are;
- Integrated platform for all data management, analysis and graphics
- Allows for reproducible research
- Active community of users providing support
- Cost = Free!
2.1.1 Installing R and RStudio
R
comes bundled with an interface but not many people use it. The vast majority of R
users actually use RStudio to work with R including us. RStudio provides a lot of useful features such as making it easier to see what you’ve done so far and view the help menu.
There are two options at the time of writing; installing R
and RStudio on your personal computer (local) or using an online version (cloud). Either is fine. The cloud version is, at the time of writing, free but this is likely to change. Also the cloud version can be slow. Basically, if you are uncomfortable or unable to install programs on your computer, then use the cloud version. Otherwise, the local version is the best choice.
2.1.1.1 Local
We recommend following the “Download, Install and Setup R and RStudio” tutorial on https://www.statslectures.com/r-stats-videos-tutorials/getting-started-with-r
The direct link is here; https://youtu.be/riONFzJdXcs
2.1.1.2 Cloud
As of the time of writing, RStudio is providing a free version of their software that you can run in your web-browser. Go to https://rstudio.cloud and sign up as you would most online accounts.
One thing to note is getting data onto the cloud requires an extra step. You’ll need to press Upload
in the bottom right panel, shown below, and browse your computer for the files. Also, if you want to upload multiple files at once, you need to first “zip” these files together and upload this file.
2.2 Get the data
The data sets can be currently found at the following link.
In case the link is broken, the updated link is likely to be found by navigating to https://stats4sd.org/resources, searching “Online R Course” and downloading Data.zip
.
2.4 R Markdown
R Markdown are documents that allow you to write text around your code and output your work into multipile formats, including a HTML, PDF or Word.
The “text mode” is similar to your experience with typing documents in Word or Notepad. Technically it uses “Markdown”, so you have the option for tables and numbered lists in your final output.
Code in R Markdown is implemented in “chunks”. There are two types of chunks, in-line and blocks. To insert a chunk, you need to press Insert -> R
located at the top right of the “Source” panel.
Once done, enter the code below and press the green play button on the right hand side of the chunk. In this tutorial, code appears in a light grey box and output in a white box.
Code
2+2
Output
## [1] 4
Hopefully when you run the code, you see that 2+2 still equals 4!
The code chunks in R Markdown start with ```{r}
and ends with ```
.
Do not modify these lines of the chunk otherwise bad things might happen. Modify anything in the middle.
2.6 Getting the files
For this tutorial you will need to download a collection of files. These can be found at here. One you have downloaded them, you will need to extract them.
2.5 Comments
Using
#
in a code block stops the anything after the symbol being run.#
is referred to as “commenting” code because it’s usually used to add commentry to long pieces of code. Try running the code below.QUESTION: What do you think the code below will output? Why?