Directions

For this lab, you will create a single .R file called lab03.R. The following exercises will ask you to write code. Place all requested code in this .R file separated by comments which indicate which code corresponds to which exercise.

Submit your lab to the corresponding assignment on Canvas. You have unlimited attempts before the deadline. Your final submission before the deadline will be graded.


Grading

This lab will be graded based on a mix of correctness and completion. For each exercise that you demonstrate a good-faith effort to complete, you will receive at least one point.


Exercise 1 (Working with Vectors)

Run and add the following four lines to your .R file.

set.seed(42)
x = rnorm(100)
y = sample(letters, size = 1000, replace = TRUE)
z = sample(c(TRUE, FALSE, NA), size = 100, replace = TRUE)

Write three additional lines of code:


Exercise 2 (Working with Lists)

The following code (which you should include in your .R file) creates a truly absurd list.

absurd_list = list(
  q = "Look elsewhere.", 
  y = list(
    z = list(x = 1),
    x = list(x = 2),
    zzz = list(
      zzz = "Not here.",
      zz = list(qq = "gg"),
      a = list(
        b = list(
          q = list(
            answer = list(
              answer = "Hello World!")
      )))
    )
))

Using only brackets and integers, write a single line of code that extracts the atomic vector of length one named answer containing Hello World! nested deep inside this list.


Exercise 3 (Working with Data Frames)

The airquality dataset comes pre-loaded in R.

head(airquality)
##   Ozone Solar.R Wind Temp Month Day
## 1    41     190  7.4   67     5   1
## 2    36     118  8.0   72     5   2
## 3    12     149 12.6   74     5   3
## 4    18     313 11.5   62     5   4
## 5    NA      NA 14.3   56     5   5
## 6    28      NA 14.9   66     5   6

Do two things to this data frame:

Store your result in a data frame named answer.

Because these are the only two columns that contain NA values, you can check your work by comparing your result to na.omit(airquality). Specifically, you can run:

all(answer == na.omit(airquality))

Done correctly, this should return TRUE. (We cannot use the identical() function here because running na.omit() adds some attributes to the data frame it returns.) Obviously, your solution should not use na.omit().


Exercise 4 (2021 Illini in the NFL)

For this exercise and the next, we will utilize data provided by the nflreadr package.

Assuming you do not have this package installed, you can do so by running:

install.packages("nflreadr")

Do not include this installation code in your .R file.

The following two lines of code will read in data on NFL rosters for the 2021 season. Include these lines in your .R file.

library(nflreadr)
rosters_2021 = as.data.frame(load_rosters(seasons = 2021))

The data are stored in rosters_2021 as a data frame. (Here we are coercing to data frame as the data is initially loaded as a data.table which we will address later in the semester.)

When using “real” data, it is always best to find a data dictionary to better understand what the rows and columns of the data represent.

Do the following:


Exercise 5 (2021 Chicago Bears Schedule)

The following code (which you should include in your .R file) loads data on the 2021 NFL schedule.

nfl_2021 = as.data.frame(load_schedules(seasons = 2021))

Write code that extracts the Chicago Bear’s schedule from this data. The result should be a data frame with 17 rows. To make the output easy to read, only include columns for the week, away team, and home team.