library(tidyverse)
PA 8: The 12 Days of Christmas
Starter Functions
Introduction
The song “12 Days of Christmas”, written around 1780, tells the tale of many gifts a person receives in the days leading up to Christmas (link to lyrics. You may want to listen to the song.
These gifts repeat and compound; on the first day, the narrator receives
A partridge in a pear tree.
On the twelfth day, they receive
Twelve Drummers Drumming
Eleven Pipers Piping
Ten Lords a Leaping
Nine Ladies Waiting
Eight Maids a Milking
Seven Swans a Swimming
Six Geese a Laying
Five Golden Rings
Four Calling Birds
Three French Hens
Two Turtle Doves
And a Partridge in a Pear Tree
This week, your task will be to write functions that automatically sing this very repetitive song. In the practice activity, we will start by writing two helper functions which we will use in the lab to write a function to sing this entire song.
Data set
Run the code provided to load in a data set called xmas
that contains the crucial information about the gifts in the song. We will use this data set to test out our functions as we work on them.
<- read.csv("https://github.com/earobinson95/stat331-calpoly/raw/master/practice-activities/data/xmas.csv") |>
xmas ::clean_names() janitor
Your functions can - and should! - reference each other. That is, don’t duplicate code; use earlier, smaller functions inside your larger functions.
Make smaller versions of the
xmas
data set (e.g., the first two days).Once you feel confident in your function code, use the smaller version of the data to test the functions you write, before you test them on the full data set.
Don’t sweat the small stuff, we will build off of the practice activity and use iteration in the lab this week. Then you will have a chance to “polish” the song for Flex Points!
1 Plurals – pluralize_gift()
The gifts are listed in singular: for example, on day five the narrator receives “five golden rings”, but the entry in the data set for the gift on day five simply says “ring”.
Hint 1: The gifts on days six and nine have unusual pluralization. You may assume that in other data sets, there will be no additional special cases besides these types and that “moose” would become “meese”.
Hint 2: The following small examples may be useful to you:
<- c("Kimberly", "Trini", "Jason", "Billy", "Zach", "Tommy")
my_names str_c(my_names, "s", sep = "")
[1] "Kimberlys" "Trinis" "Jasons" "Billys" "Zachs" "Tommys"
str_replace(my_names, "y$", "ies")
[1] "Kimberlies" "Trini" "Jason" "Billies" "Zach"
[6] "Tommies"
You should absolutely not “hard-code” anything into this function; this function should work in general, not just for the items in The 12 Days of Christmas. For example, the word “rings” should not appear anywhere in the function. I should be able to give it any gift and get back the plural of that gift.
Using the skeleton of the pluralize_gift()
function, complete the code so that the function takes a gift and returns the appropriate plural.
# Function that takes a noun and makes it plural
# Arguments -- gift -- A string or vector of strings
# Return -- A string or vector of strings with the pluralized words
# Hint -- the stringr functions will be incredibly handy here!
<- function(gift){
pluralize_gift
<- # your code to modify the gift here
gift
return(gift)
}
Try your function out on the smaller and then larger gift data set. Consider: is your function vectorized? It does not have to be, but you can try it out if you want!
# Will work if your function is vectorized!
pluralize_gift(gift = xmas$gift_item)
[1] "partridge" "dove" "hen" "bird" "ring" "goose"
[7] "swan" "maid" "lady" "lord" "piper" "drummer"
# Will work if your function is not vectorized!
map_chr(.x = xmas$gift_item,
.f = ~ pluralize_gift(gift = .x)
)
[1] "partridge" "dove" "hen" "bird" "ring" "goose"
[7] "swan" "maid" "lady" "lord" "piper" "drummer"
2 Creating sentences – make_phrase()
Write a function called make_phrase()
that takes as input the necessary information, and returns a phrase. For example,
make_phrase(day_num = 4,
adjective = "calling",
item = "birds",
verb = "",
location = "")
should return
"four calling birds"
and
make_phrase(day_num = 10,
adjective = NA,
item = "lords",
verb = "a-leaping",
location = NA
)
should return
"ten lords a-leaping"
The day_in_words
variable isn’t quite what you want! For example, you want 12
to say "twelve"
not "twelfth"
or 12
. I suggest using the english
package!
<- function(day_num, adjective, item, verb, location) {
make_phrase
## Step 1: Replace NAs with blank strings
<- str_replace_na(adjective, "")
adjective
## Step 2: If the day number is larger than 1, the gift items need pluralized!
### Hint: call the function you created above!
## Step 3: Figure out if a gift item starts with a vowel
## Step 4: For the first day, if the gift item starts with a vowel, add "an" to the beginning and if the gift item does not start with a vowel, add "a" to the beginning
### For example, "a partridge in a pear tree". Pretend you could have "an eagle in a pear tree".
### If it is not the first day, use just the number word (e.g. "four golden rings" and "ten lords a-leaping")
### See tip above about how you will need to address number vs the number word (e.g. 10 vs ten vs tenth)
## Step 5: Glue all of the pieces together into one string and return!
}
Try your function out on the xmas
data, by making a new variable containing the daily phrases. Notice I’ve provided you with the code to iterate through each row of the data set to create a phrase – all you need to do is provide the necessary inputs into pmap_chr()
.
<- xmas |>
xmas_phrase mutate(full_phrase = pmap_chr(.l = list(day_num = ______,
adjective = ______,
item = ______,
verb = ______,
location = ______
), .f = make_phrase
) )
Error: <text>:2:56: unexpected input
1: xmas_phrase <- xmas |>
2: mutate(full_phrase = pmap_chr(.l = list(day_num = __
^
Your
full_phrase
column is the answer to this week’s Practice Activity.Copy and paste your
full_phrase
column to show me the phrases you made!
|>
xmas_phrase pull(full_phrase)
Error in eval(expr, envir, enclos): object 'xmas_phrase' not found