The goal of this lab is to introduce you to R and RStudio, which you’ll be using throughout the course both to learn the statistical concepts discussed in the course and also to analyze real data and come to informed conclusions.
To clarify which is which:
As the labs progress, you are encouraged to explore beyond what the labs require; a willingness to experiment will help you see how what you’re doing actually works and how the pieces fit together. Before we get to that stage, however, you need to build some basic fluency in R.
Before we start, let’s get RStudio running.
There are two ways you can run R and RStudio:
That said, I don’t recommend trying to do your own R installation yet, particularly if you’re new to code and managing IDEs, etc. (which I expect most people in this class will be).
For today, even if you have R/RStudio installed on your machine, you should log in to the server so you know how to do so.
Here’s how to do it
cdawson
)passwd
at the terminalToday we begin with the fundamental building blocks of R and RStudio: the interface, reading in data, and basic commands.
The panel in the upper right has tabs for your
The pane in the lower right has tabs for
The panel on the left is called the console. When you first launch RStudio, it shows what version of R that you’re running, etc.
Below that information is the prompt. As its name suggests, this prompt is really a request, namely a request for a command: a line of R code that tells the computer to do something.
Enter some simple arithmetic expressions like the following at the console and verify that the output is as expected. (Note that the asterisk represents multiplicationn and the slash is for division.)
2 + 5
13 * 17
17.04 / 2.8
sqrt(49)
Useful tip: When your cursor is at the console, you can use the up and down arrows to cycle through recently typed commands.
Each time you begin a new lab or assignment, you should create a new project so you can keep your work distinct from one assignment to the next.
stat113
. Click “Browse” and choose that directory as the home of your new project.lab1
. Type this name in the “Directory Name”. Avoid using spaces or special characters (such as punctuation) in directory names. For multiword names you can use CamelCase
, or separate words with-hyphens
or with_underscores
. Pick a convention and stick with it.Whenever you begin a new assignment, it is a good idea to create a new project. This will help keep things you did in a previous session from interfering with what you are trying to do in this session.
R is an open-source programming language, meaning that users can contribute packages that make our lives easier, and we can use them for free. Most of what we need for this course is contained in two particular packages: mosaic
and tidyverse
.
Both are already available on the RStudio server.
Note that you can check to see which packages (and which versions) are installed by inspecting the Packages tab in the lower right panel of RStudio. Don’t install packages yourself on the server: This could result in two different versions being installed at once, causing you to see different behavior than others in the class
We will use a document format called RMarkdown that helps us keep our work better organized. It is basically like a Word document with some stripped down formatting capability, but with an important difference: you can embed R commands into your document in “code chunks”, which will be run and produce output when you “compile” your document.
This instruction document was created using RMarkdown.
lab1.Rmd
from the stat113/originals/
folder. This will cause the left-hand part of R Studio to split in half, the console moving to the bottom, and a new pane appearing in the upper left that has a tab showing the contents of the file.lab1.Rmd
in the project directory you just created above. Make sure to double-click on the folder name so that you are viewing the contents of the folder before clicking ‘Save’.