Factors, Strings, and Dates



Time Estimates:
     Videos: 30 min
     Readings: 0-30 min
     Activities: 120 min
     Check-ins: 6



Factors


Required Video: Factors with forcats




Recommended Reading: R4DS Chapter 15: Factors




Check-In 1: Functions from forcats


Answer the questions from lecture:

  • What is the difference between fct_relevel() and fct_recode()?

  • What is the difference between fct_collapse() and fct_lump()?

  • What is the difference between fct_reorder() and fct_inorder()?

Feel free to share answers with your classmates in the Discord server, to make sure you understand.


Check-In 2: Apply your skills


In your colleges_clean dataset, adjust the REGION variable to have nicer or clearer labels and/or better categories.


Strings


Required Video: Strings with stringr




Recommended Reading: R4DS Chapter 14: Strings




Check-In 3: Functions from stringr


Answer the questions from lecture:

  • What is the difference between length() and str_length()?

  • What is the difference between str_replace() and str_replace_all()?

  • What is the difference between str_trim() and str_trunc()?

  • What is the difference between str_sub() and str_subset()?

  • What does the collapse argument do in str_c()?

Feel free to share answers with your classmates in the Discord server, to make sure you understand.


Check-In 4: Apply your skills


Using your colleges_clean dataset, answer these questions:

Question 1: How exactly does Cal Poly’s name appear in this dataset?

Question 2: How many colleges have the word “University” in their name?

Question 3: Which college has the longest name? The shortest name?


Dates


Required Video: DateTime Objects with lubridate




Recommended Reading: R4DS Chapter 16: Dates and Times



Extra Resources:


Check-In 5: Functions from lubridate


  • What is the difference between year() and years() and dyears()?

  • What time zone does R think dates are in, by default?

  • What is the difference between %within% and %--%?

  • What happens if you use the parse_datetime() function, but you do not provide a day argument?


Check-In 6: Apply your skills


Question 1: Warm-up

Answer the following using code (even if you know the answer already).

  1. Save your birthday as a Datetime object in R.

  2. What day of the week does/did your birthday fall on in 2020?

  3. On what day of the week should you plan your 100th birthday party?

  4. Find the date that is exactly 9 months before your birthday. If it falls on a holiday, think about the implications.

Question 2: Your Zodiac sign

Download the zodiac.csv data from the course website. This dataset gives the date ranges for the 12 Zodiac signs; that is, if you were born between the dates listed, you are affliated with that particular sign.

Use this dataset, and your new skills with DateTimes, to determine your sign using only code.