Lab 01 - Introduction to R and R Markdown (2024)

2024-01-26

This is an R Markdown document.

At present, you are reading it formatted as an HTML document, but aswe have seen, it can also be rendered as a PDF – when working with an.Rmd file, you can choose how it is presented by selecting the arrownext to the Knit ball of yarn an choose between “Knit to HTML” or “Knitto PDF”. For most of the work in this class, we will be knitting to aPDF to facilitate submissions to Gradescope.

When you Knit an R Markdown file, it will ask you to first save andname your document. I recommend sticking to a consistent formatthroughout the semester: something like “lastname_firstname_lab/hwk#”.For example, I would name this “nolte_collin_lab1.Rmd”. After you knit,this will also be the name of your pdf.

For this lab, we will be following along with this document which wecan begin by downloading the .Rmd file associated with it here; this is allow us to see side-by-side howthe .Rmd file looks compared to the finished output.

For example, reading this online we see that this sentence isbold, while in the .Rmd file we see that the sentence is aplain-text sentence surrounded by two asterisks (*) on each side. Thisis an example of using Markdown toformat our documents. For this lab, we will be following along with the.Rmd file, filling bits in as prompted. Let’s begin by introducing someof the more common markdown syntax that we will use in this class.

Text Editing

We can use .Rmd files to write text just as we would with a standardword processor. As we saw previously, using twoasterisks creates bold text, whereas using singleasterisks creates text that is italicized.

Headers can be created using the # symbol. Using one creates a largeheader, two a smaller header, and so on. You can see examples of this inthe text above. Take note that you need to leave a space between the #and the header text – forgetting to do so will render the text exactlyas you have written it.

#For example, this will not show up as a header

We can also create sequential lists. To do so, start new lines withsequential numbers

  1. Like this
  2. And this
  3. And finally this

We can create un-ordered lists with using stars or dashes

  • Here is an item
  • Here is another item
    • And if I indent, I can make nested lists

We can also create horizontal dividers with a line of stars (at least3, but any number greater is ok). This is a good way to separate youranswers from a given problem

Things are more clear if I put my answers between bars, but I need tobe sure to leave a blank line between my text and the bottom star

Question 1:

Between the stars below, do the following:

  1. Use two # to create a header that says About Me
  2. Type your first name in bold and your last name in italics
  3. Create a bullet point list of the people sitting on either side ofyou
  4. Create a numbered list of your 3 favorite books or movies

Once you have done this, Knit to PDF by clicking the bar of yarnabove and verify that everything looks like it should.

Whereas R is the programming language responsible for doing ourrelevant computation, our interactions with R will be primarily throughR Studio. When you first open R Studio, your screen will partitionedinto 3 panes: Console, Environment, and Files(and often, Editor)

Console

The console makes up the entire left-hand side of the screen, and itis here that we can interact with R directly. Some information willappear on start-up, followed by a prompt (indicated with>). Try copying and pasting the following lines intoyour console one at a time and pressing enter

5 + 2

sqrt(25)

pi

(3^2 + 1)*5

x <- 5

Environment

The environment panel is located in the top right and includes all ofthe named variables that we have created in R. If you copied the linesabove correctly, you should see a single line here, indicating that thevariable x has the value 5. When starting anew R session, this pane will be empty.

Files

In the bottom right is the Files pane which, in addition to giving usinformation about our file system also includes tabs for Plots and Help.Copy each of the lines below to see how this pane changes

?sqrt

plot(1:10)

You can click any of the tabs in this pane to change what iscurrently being shown.

Editor

Finally, we have the editor. If you start a new R Studio session,there will be nothing in the editor, but this will change if we openeither a .R or .Rmd file. An R Script is the simplest type of file whichcan be created from the menus at the top:

File -> New File -> R Script

The editor pane will appear with a blank R file in which you canwrite and store R code. Try copying the R commands from theConsole section into an R Script, click on the line with codeand click “Ctrl+Enter” on your keyboard. R Studio should execute thiscode and display the results in the Console. You do not have to savethis file, and you can close it when you are finished.

While it is true that R scripts (files that end in .R) are used forthe vast majority of written R code, we will primarily be using RMarkdown files (files that end in .Rmd).

Perhaps the most obvious utility in using R Markdown documents forthis course is their ability to interweave regular text with R. We caninclude and run R code by including it in a code chunk, such asthe one shown below:

# This is a code comment, beginning with #x <- 4 x^2
## [1] 16

This code will run and display its results when we push the Knitbutton, but we can also run the code without knitting by place by againclicking the line with our cursor and hitting “Ctrl+Enter”

For this portion of the lab, we will be introducing some of the basicmechanics in the R language. This will include vectors,similar to the variables we discussed in class, as well asdata.frames, the primary data type we will be using forcollections of observations.

You are welcome to follow along on the course website or read the RMarkdown directly. In either event, I highly encourage you to tryrunning the code that we see, either by coping it from the website andpasting into the console, or by running with the R Markdown codechunks.

Vectors

Creating and subsetting

Vectors in R are the simplest type of object. Every vector has twoattributes that will dictate how we use it: length and type. We havealready seen vectors of length one, which are created by assigning anumber to a name using <-

# This is a numeric vector of length 1x <- 4x
## [1] 4

We can create vectors of greater than length one usingc(), which is short for “combine”

y <- c(2, 4, 6, 8, 10)y
## [1] 2 4 6 8 10

Sequences of vectors can also be created with :

z <- 1:5z
## [1] 1 2 3 4 5

Every element of a vector has a position that we can access directlyusing square brackets [] in a process calledsubsetting. Here, we grab the 3rd element of the vectorw

w <- c(3, 6, 9, 12)w[3]
## [1] 9

Question 2:

Let’s practice creating vectors and subsetting with a shortexercise.

  1. First, create an R code chunk between the rows of stars below(Ctrl+Alt+I is quick way to do this)
  2. Next, create a vector called x that has all of thenumbers from 1 to 10
  3. Use square brackets and subsetting to select all of the evennumbers

Vector types

Just as we saw in class that variables can be quantitative orcategorical, so it is with vectors in R. There are primarily 3 types ofvectors we need to be aware of:

  1. numeric - for example: x <- c(1, 2, 3)
  2. character - for example:x <- c("a", "b", "c")
  3. logical - for examplex <- c(TRUE, FALSE, TRUE)

Don’t worry about memorizing everything – we will continue to reviewthem together as they appear in the course. For now, we will besatisfied with simply having made our introductions.

As we mentioned in class, the type of a variable will often dictatewhat we are able to do with it. For example, it should make sense totake the mean of a numeric variable, but less so with a charactervector:

# This will return a warning and NAz <- c("a", "b", "c")mean(z)
## Warning in mean.default(z): argument is not numeric or logical: returning NA
## [1] NA
x <- c(1, 2, 3)mean(x)
## [1] 2

Some situations can be confusing based on appearances. Theclass() function will tell us what type a vector is if weare not sure

# This looks numericz <- c("1", "2", "3")mean(z)
## Warning in mean.default(z): argument is not numeric or logical: returning NA
## [1] NA
class(z)
## [1] "character"

Usually in situations like this, we can coerce a vector tobeing a different type

z_num <- as.numeric(z)class(z_num)
## [1] "numeric"
mean(z_num)
## [1] 2

One important characteristic of a vector to note: all elements of avector must be the same type. They are either all numeric, alllogical, or all character.

Question 3:

Can you think of a situation in which we start with a numeric vectorthat we would like to coerce to a character vector? Write your answer inthe space below:

Data Frames

Most of the data we will be working with in this class will be storedin objects known as data frames. Put simply, a data frame is acollection of vectors that are all the same length.

# Create two vectors of the same lengthx <- c("Alice", "Mary", "Bob")y <- c(1, 4, 10)# We can create a data frame with columns named `name` and `value`df <- data.frame(name = x, value = y)df
## name value## 1 Alice 1## 2 Mary 4## 3 Bob 10

As we can see, this has the format of having rows as observations andcolumns being variables. We can select one of the variables from ourdata.frame using $ which will extract thevector

# Extract the "name" columndf$name
## [1] "Alice" "Mary" "Bob"
# Extract the "value" column and use it to find medianmedian(df$value)
## [1] 4

While we can create data frames directly, it is far more likely thatwe will be reading into R data that has already been created. The mostcommon way to do so is with the read.csv() function (CSVstands for comma separated values). This can be read directly from yourcomputer OR you can pass in a URL

# Use read.csv to pull Happy Planet dataplanet <- read.csv("https://remiller1450.github.io/data/HappyPlanet.csv")

(Images in this next section will appear online but not on your owncomputer as you do not have these images)

Once a data frame is read in, you will see it show up in yourenvironment. Right away, we get two critical pieces of information: thisdata frame has 143 observations and 11 variables.

Lab 01 - Introduction to R and R Markdown (1)

If we click on the blue arrow to the left, we will get a drop downshowing us a more detailed description of the variables we do have. Notealso the additional information given about the vectors (num = numeric,int = integer, chr = character).

Lab 01 - Introduction to R and R Markdown (2)

Clicking on the white box pane to the right of planetbrings us to the tabular view in R, giving us the opportunity tonavigate and explore the data more directly

Lab 01 - Introduction to R and R Markdown (3)

Question 4:

For this question, we will be using the HappyPlanet datathat we have just read in:

  • Part A Looking at the Happy Planet data, explainin one or two sentences what consitutes an observation in thisdataset

  • Part B Using $ to extract columnsfrom the dataset, what is average life expectancy of countries in thedataset?

  • Part C Are there any variables in this datasetthat are stored as a numeric that would be better suited as acategorical variable? Explain your answer

Wrapping Up

This concludes our first introduction to R! Once you have finishedwith your lab, make sure that you are able to successfully Knit to PDF.Once that is finished, we are ready to submit to Gradescope.

If you are in STA-209-03 (1-2:20), the Entry Code is B272WD

If you are in STA-209-05 (2:30-3:50), the Entry Code is B2WB5B

Lab 01 - Introduction to R and R Markdown (2024)

References

Top Articles
Latest Posts
Article information

Author: Arielle Torp

Last Updated:

Views: 6621

Rating: 4 / 5 (41 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Arielle Torp

Birthday: 1997-09-20

Address: 87313 Erdman Vista, North Dustinborough, WA 37563

Phone: +97216742823598

Job: Central Technology Officer

Hobby: Taekwondo, Macrame, Foreign language learning, Kite flying, Cooking, Skiing, Computer programming

Introduction: My name is Arielle Torp, I am a comfortable, kind, zealous, lovely, jolly, colorful, adventurous person who loves writing and wants to share my knowledge and understanding with you.