In this lab you can use the interactive console to explore or Knit the document. Remember anything you type here can be “sent” to the console with Cmd-Enter (OS-X) or Ctrl-Enter (Windows/Linux) in an R code chunk.

Part 1

1.1

Read in SARS-CoV-2 vaccination data from URL http://jhudatascience.org/intro_to_r/data/vaccinations.csv and assign it to an object named vacc.

# General format
library(readr)
# OBJECT <- read_csv(FILE)
library(tidyverse)
vacc <- read_csv(file = "http://jhudatascience.org/intro_to_r/data/vaccinations.csv")
## Rows: 37272 Columns: 103
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr   (2): Date, Location
## dbl (101): MMWR_week, Distributed, Distributed_Janssen, Distributed_Moderna,...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

1.2

Filter the dataset so that the “Location” column is equal to “US” (aka the entire USA). Store the modified dataset as vacc_filtered.

# General format
NEW_OBJECT <- OBJECT %>% filter(COLUMNNAME == CRITERIA)
vacc_filtered <- vacc %>% filter(Location == "US")

1.3

Write out the vacc_filtered object as a CSV file calling it “vacc_filtered.csv”, using write_csv():

write_csv(vacc_filtered, file = "vacc_filtered.csv")

Practice on Your Own!

P.1

Copy your code from problem 1.3 and modify it to write to the data directory inside your R Project. Note: you may need to make a new folder named “data” if it doesn’t already exist.

getwd()
dir.create("data")
write_csv(vacc_filtered, file = "data/vacc_filtered.csv")

P.2

Write one of the objects in your Environment to your working directory in rds format. Call the file my_variable.rds.

y <- c(10, 20, 30, 40, 50, 60)
write_rds(y, file = "my_variable.rds")

P.3

Read the RDS file from your working directory back into your Environment. Call the file z.

z <- read_rds(file = "my_variable.rds")