The goal this week is to introduce R and RStudio which will be used throughout the course both to review the statistical concepts discussed in the course and to analyze real data and come to informed conclusions. To clarify which is which: R is the name of the programming language itself, and RStudio is a convenient interface.
Today, we begin with the fundamental building blocks of R and RStudio: the interface, creating and saving files, and basic commands.
Open Appalachian’s RStudio Server and sign in: RStudio Server
Your credentials are the same as for your email.
Please DO NOT choose Stay signed in.
In RStudio Server, you should see a window that looks like the image in Figure 2.1.
Figure 2.1: The RStudio interface
The panel on the left is where the action happens. It’s called the console. Every time RStudio is launched, it will have the same text at the top of the console describing the version of R that is running.
The panel in the upper right contains the workspace. This shows the variables and objects defined during an R session and a history of the commands that are entered.
Any plots that are generated will show up in the panel in the lower right corner. This is also where you can browse your files, access help files, and upload and download files.
In R, we use a document type called an R Markdown document. R Markdown documents are useful for both running code and annotating the code with comments. The document can be saved, so you can refer back to your code later and can be used to create other document types (html, word, pdf, or slides) for presenting the results of your analyses. R Markdown provides a way to generate clear and reproducible statistical analyses.
To open a new file, click on the little green plus on the upper left hand and select R Markdown; see Figure 3.1. You can leave it untitled.
Figure 3.1: Starting a new R Markdown file
When you open a new R Markdown file, there is some example code in it that you can delete. We will take care of this next.
Let’s make some changes to the R Markdown file you just opened. Using the image below as a guide,
Your final result should look like this:
Lab work is committed and pushed as an R Markdown file like this each week, so it is important to learn how to save these files.
PS01_lastname_firstname
(fill in your firstname and lastname)This is now saved in the MD-PS01-SC
folder on the server.
Click the Knit button at the top left side of the screen to “knit” the file, or in other words, produce an output document. An .html
file will be generated. It is automatically saved in the same folder as your R Markdown file.
Note that there is now a R Markdown file (.Rmd
) and an html file (.html
) in the MD-PS01-SC
folder.
Inspect the .html
file to see how what you typed was formatted. There are lots of tricks for controlling the formatting of the knitted html file. For instance:
##
and a space in front of text makes it into a large header. For example, see how ## This is a header
in your R Markdown .Rmd
file translates in the resulting .html
output.###
and a space in front of text makes it a smaller header.The code chunks are where you put R code in an R Markdown file. So far, your “knitted” file (your output document file) doesn’t show anything, because we did not put any content in the code chunks yet.
Using your first code chunk, type the following command to create a new variable called x
with the value of 6.
x <- 6
The arrow <-
is called an ASSIGNMENT OPERATOR and tells R to save an object called x
that has the value of 6. This is similar to saving a value in a graphing calculator.
Note that whatever you want to save must always be to the left of the assignment operator.
To actually RUN this command in your console, you have a few options:
Control-Enter
on a PC or Command-Return
on a MacThink of “running” code in your console as telling R “do this.”
Note that you now have a new object in your workspace, called x
.
So far, you have made a numeric variable x
. There many other types of data objects you can make in R.
First, copy, paste, and run the following command in a new code chunk to make a character called favorite_movie
. Think of characters as text as opposed to numerical values. Note that R knows this is a character because there are quotation marks around Star_Wars
.
favorite_movie <- "Star_Wars"
Next, copy, paste, and run the following command in a new code chunk.
v <- c(2, 4, 6)
This makes what is called a vector, which we have named v
. It is a data object that has multiple elements of the same type. This vector contains three numbers, 2, 4, and 6. The c()
function says tells R to concatenate
the values 2, 4, 6, into a single vector. Note in the Environment pane that your vector v
contains numbers (listed as num
).
You can do math on a vector that contains numbers. For instance, copy, paste, and run the following command in a new code chunk. This tells R to multiply each element of the vector v
by 3.
v * 3
Type complete sentences to answer all questions inside the answer
tags provided in the R Markdown document. Round all numeric answers you report inside the answer tags to four decimal places. Use inline R
code to report numeric answers inside the answer
tags (i.e. do not hard code your numeric answers).
Remember to save your work as you go along. Click the save button in the upper left hand corner of the R Markdown window.
y
with the value of 7x
by y
, and store the answer in a variable named z
like so: z <- x * y
favorite_movie
, x
, v
, y
, and z
all in your Environment pane# Type your code and comments inside the code chunk
y <- 7;
z <- x * y;
6 + 3
# Type your code and comments inside the code chunk
6 + 3;
[1] 9
Answer appears in a line labled [1] under the code chunk that ends on line 190 and before the line of code on on line 191.
6 + 3
as a variable called a
.a
? (please answer with text)a
into the code chunk and re-run the code chunk. What happens? (please answer with text)# Type your code and comments inside the code chunk
a <- 6 + 3;
a
[1] 9
The answer does not appear anywhere because it a stores the value of 6 + 3. The value of the object a is seen in the environment. After typing a into the code chunk and re-running the code chunk it places the value of a in a line under the code chunk and before the code on line 207.
It is a good idea to try kitting your document from time to time as you go along. Go ahead, and make sure your document is knitting and that your html file includes Exercise headers, text, and code. Note that knitting automatically saves your Rmd file.
a^2
.^
operator do? (please answer with text)# Type your code and comments inside the code chunk
a^2
[1] 81
The ^ operator is used to raise something to a power.
sum(a, x, y)
sum
is a function. Based on the output, what do you think the sum
function does? (please answer with text)# Type your code and comments inside the code chunk
sum(a, x, y)
[1] 22
The sum function adds things together, in this case variables.
It removed everything from the environment tab.
It runs all code chunks in the entire file.
v
we created earlier. Copy, paste, and run the following in a code chunk. What does this code accomplish? (please answer with text)v + 2
# Type your code and comments inside the code chunk
v + 2
[1] 4 6 8
It created a vector v with in this case three entries stored in it. Each time v + 2 is coded and run it will add an entire to the vector. It will take the last entrie and add two to it.
music
that contains music genres. Recall a vector is a data object that has multiple elements of the same type. Here the data type is a character. Look in the environment pane. How does R tell us that this vector contains characters not numbers? (please answer with text)music <- c("bluegrass", "funk", "folk")
# Type your code and comments inside the code chunk
music <- c("bluegrass", "funk", "folk")
The R tells us that the vector contains a vector by using the abbreviation chr to mean character(s).
Italicize like this
Bold like this
A superscript: R2
This is a trick question. Charlie Brown has nothing to do with R.
You will need to make sure you commit and push all of your changes to the github education repository where you obtained the lab.
</div>
tags, and delete the extraneous comments.There are a lot of stats classes and students using the Server. To keep the server running as fast as possible, it is best to sign out when you are done. To do so, follow all the same steps for closing an R Markdown document as above:
sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: Red Hat Enterprise Linux
Matrix products: default
BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] knitr_1.33
loaded via a namespace (and not attached):
[1] bookdown_0.23 png_0.1-7 digest_0.6.27 R6_2.5.0
[5] jsonlite_1.7.2 magrittr_2.0.1 evaluate_0.14 highr_0.9
[9] stringi_1.7.3 rlang_0.4.11 jquerylib_0.1.4 bslib_0.2.5.1
[13] rmarkdown_2.10 tools_3.6.0 stringr_1.4.0 jpeg_0.1-9
[17] xfun_0.25 yaml_2.2.1 compiler_3.6.0 htmltools_0.5.1.1
[21] sass_0.4.0