Day 1B Practice

Question 1.

  1. Create a character vector named flavors that contains the following strings and print it:
    • Cookies & Cream
    • Americone Dream (R)
    • Bob Marley’s 1 Love
  2. Use a function to calculate the number of strings in flavors.
  3. Use another function to calculate the number of characters in each string in flavors.
  4. Let R know how you feel about ice cream by using a function to either make all the characters in flavors uppercase (you LOVE ice cream) or lowercase (you don’t love ice cream).

Click here for the answer key

Answer (a)

flavors <- c("Cookies & Cream", "Americone Dream (R)", "Bob Marley's 1 Love")
flavors
#> [1] "Cookies & Cream"     "Americone Dream (R)" "Bob Marley's 1 Love"

Answer (b)

length(flavors)
#> [1] 3

Answer (c)

nchar(flavors)
#> [1] 15 19 19

Answer (d)

toupper(flavors)
#> [1] "COOKIES & CREAM"     "AMERICONE DREAM (R)" "BOB MARLEY'S 1 LOVE"
tolower(flavors)
#> [1] "cookies & cream"     "americone dream (r)" "bob marley's 1 love"

Question 2.

The following table summarizes the season information for the eight seasons of AMC’s Breaking Bad show.

  1. Tidy up this data and save it to a tibble. Decide for yourself how to handle season 5 (should it be a single observation or two?). For the first and last aired dates, just store the year as a number.

  2. Save the tibble you created to a CSV file named “breaking_bad.csv”.

Click here for the answer key

Answer (a)

Version with one observation for the season five parts

library(tidyverse)
season <- c(1, 2, 3, 4, 5)
episodes <- c(7, 13, 13, 13, 16)
first_air <- c(2008, 2009, 2010, 2011, 2012)
last_air <- c(2008, 2009, 2010, 2011, 2013)
network <- "AMC"
breaking_bad <- 
  tibble(season, episodes, first_air, last_air, network)
breaking_bad

Version with two separate observations for the season five parts

library(tidyverse)
season <- c(1, 2, 3, 4, 5.1, 5.2)
episodes <- c(7, 13, 13, 13, 8, 8)
first_air <- c(2008, 2009, 2010, 2011, 2012, 2013)
last_air <- c(2008, 2009, 2010, 2011, 2012, 2013)
network <- "AMC"
breaking_bad <- 
  tibble(season, episodes, first_air, last_air, network)
breaking_bad

Answer (b)

write_csv(breaking_bad, "breaking_bad.csv")

Resources

Fun Stuff

“Who’s” on First?

A lesson about the importance of strings…

Fundamentals of Tidying

She doesn’t like formatting-as-data either…