Lecture 03a Practice

These activities are for practice only — there is nothing to turn in. Answer keys are included, so try each question yourself before opening them.

Question 1

  1. Create a character vector named flavors that contains the following strings and print it to the console:
  • Cookies & Cream
  • Americone Dream (R)
  • Bob Marley’s 1 Love
  1. Use a function to calculate the number of strings in flavors.
  2. Use another function to calculate the number of characters in each string in flavors.
  3. 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).

Answer key

# Part (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"

# Part (b)
length(flavors)
## [1] 3

# Part (c)
nchar(flavors)
## [1] 15 19 19

# Part (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

Ebi has tracked her spending over two weeks. Every time she makes a purchase on items that are not food, she writes down a code representing the day of the week (e.g., 1 = Sunday and 2 = Monday). At the end of the week, she has the following codes written down: 1 2 2 4 7 1 1 5 6 7.

  1. Create a numerical vector (not a factor) called ebi_num that contains these codes. Print it to the console.
  2. Turn ebi_num into a factor object called ebi_fct with levels 1, 2, …, 7 and labels “Sun”, “Mon”, …, “Sat”. Print it to the console.
  3. Use the table() function on ebi_num and then on ebi_fct. Compare their output. Which version do you prefer and why?

Answer key

Answer (a)

ebi_num <- c(1, 2, 2, 4, 7, 1, 1, 5, 6, 7)
ebi_num
 [1] 1 2 2 4 7 1 1 5 6 7

Answer (b)

ebi_fct <- factor(
  ebi_num, 
  levels = c(1, 2, 3, 4, 5, 6, 7), 
  labels = c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
)
ebi_fct
 [1] Sun Mon Mon Wed Sat Sun Sun Thu Fri Sat
Levels: Sun Mon Tue Wed Thu Fri Sat

Answer (c)

table(ebi_num)
ebi_num
1 2 4 5 6 7 
3 2 1 1 1 2 
table(ebi_fct)
ebi_fct
Sun Mon Tue Wed Thu Fri Sat 
  3   2   0   1   1   1   2 

I prefer the factor version because it includes the labels at the top, which is convenient, and it also includes the zero count for “Tue”, whereas the numerical version omits it. This makes it easier to see that Tuesdays had the fewest purchases for Ebi.

Question 3

  1. Install the tidyverse package. It may take several minutes to install because it contains several sub-packages that we will use in this class.
  2. Check if the tidyverse package has a website and/or vignettes. Read the “Welcome to the tidyverse” vignette (it is relatively brief and will prepare you for the next chapter).

Answer key

# Part (a)
install.packages("tidyverse")
# or RStudio > Extras pane > Packages tab > Install button

# Part (b)
browseVignettes("tidyverse")
# or RStudio > Extras Pane > Packages tab > search for tidyverse > click globe icon > check website for Articles

https://tidyverse.tidyverse.org/articles/paper.html


Note that there is no need to turn in Activities. These are just for practice!