Be sure to change the author in the YAML to your name. Remember to keep it inside the quotes.
Questions that require the use of R will have an R code chunk below it.
Download the datasets about word count in the Lord of the Rings trilogy (titled The_Fellowship_Of_The_Ring.csv, The_Two_Towers.csv, and The_Return_Of_The_King.csv) and LOTR-Table.png from the Canvas page for this assignment and save these files to the folder where the RMD file is located.
There is less hand-holding in this assignment.
For this Challenge Problem assignment, you are going to be using data from the Lord of the Rings Trilogy on total number of words spoken, by characters of different races and sexes. The data provided are stored by movie (1st movie: The Fellowship of the Ring; 2nd movie: The Two Towers; 3rd movie: The Return of the King).
Practice Problems
Now let’s practice data wrangling (in particular, reshaping).
lotr1 (for “Lord of
The Rings”). Hint: Because these three datasets all have completely
different observations (no overlap between datasets), you can just use
the bind_rows() function to do this.lotr1 <- bind_rows(fellowship, two_towers, return_king)
lotr1
lotr1 dataset so that Sex is a
single column and word count, Words, is a single column.
Save this new dataset in an object called lotr2. The
following image shows the first 6 rows of the desired dataset (there
should be 18 rows in the actual dataset):
lotr2 <- lotr1 %>%
pivot_longer(cols = c(Female, Male),
names_to = "Sex",
values_to = "Words")
lotr2
lotr2 dataset so that the columns the
number of words spoken in each movie by each race (still separate rows
for male and female, i.e. Sex as a variable). The following image shows
the desired dataset:
reshaped_lotr2 <- lotr2 %>%
pivot_wider(id_cols = c(Film, Sex),
names_from = Race,
values_from = Words)
reshaped_lotr2
Putting It All Together
Now let’s put the reshaped datasets to use (in addition to other data verbs and data visualization) to answer questions.
Question #1: Of all the words spoken, what is the total number and proportion of words spoken by female elves in Lord of the Rings?
female_words <- lotr2 %>%
filter(Race == "Elf", Sex == "Female") %>%
summarise(word_count = sum(Words)) %>%
pull(word_count)
total_words <- lotr2 %>%
summarise(word_count = sum(Words)) %>%
pull(word_count)
female_proportion <- female_words / total_words
tibble(
Category = c("Female Elves", "Total"),
Word_count = c(female_words, total_words),
Proportion = c(female_proportion, 1)) %>%
gt() %>%
tab_header(title = "Female Eleves in Lord of the Rings:",
subtitle = "Total number and proportion of words spoken ") %>%
cols_label(
Word_count = "Word Count",
Proportion = "Proportion") %>%
fmt_percent(columns = Proportion, decimals = 2)
| Female Eleves in Lord of the Rings: | ||
| Total number and proportion of words spoken | ||
| Category | Word Count | Proportion |
|---|---|---|
| Female Elves | 1743 | 8.20% |
| Total | 21245 | 100.00% |
Question #2: Does a certain race dominate the words spoken a single movie? Does the dominant race differ across movies?
race_movie <- lotr2 %>%
group_by(Film, Race) %>%
summarise(word_count = sum(Words), .groups = "drop")
ggplot(race_movie, aes(x = Race, y = word_count, fill = Race)) +
geom_col() +
facet_wrap(~ Film, scales = "free_y") +
scale_fill_paletteer_d("ggsci::default_ucscgb") +
labs(
title = "Lord of the Ring Movies: Total Words Spoken",
subtitle = "Comparing which race speaks the most",
x = "Race", y = "Total Words Spoken") +
theme_minimal() +
theme(
legend.position = "none",
plot.title = element_text(face ="bold", size = 16, hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5))
sex <- lotr2 %>%
pivot_wider(id_cols = c(Film, Race),
names_from = "Sex",
values_from= "Words")
race_sex <- sex %>%
pivot_wider(id_cols = Film,
names_from = Race,
values_from= c(Female, Male))
race_sex %>%
gt() %>%
tab_header(title = "Words Spoken by Characters (Sex, Race) in LOTR") %>%
tab_spanner(label = "Female Characters", columns = c(Female_Elf, Female_Hobbit, Female_Man)) %>%
tab_spanner(label = "Male Characters", columns = c(Male_Elf, Male_Hobbit, Male_Man)) %>%
cols_label(
Film = "Film",
Female_Elf = "Elf",
Female_Hobbit = "Hobbit",
Female_Man = "Human",
Male_Elf = "Elf",
Male_Hobbit = "Hobbit",
Male_Man = "Human")
| Words Spoken by Characters (Sex, Race) in LOTR | ||||||
| Film |
Female Characters
|
Male Characters
|
||||
|---|---|---|---|---|---|---|
| Elf | Hobbit | Human | Elf | Hobbit | Human | |
| The Fellowship Of The Ring | 1229 | 14 | 0 | 971 | 3644 | 1995 |
| The Two Towers | 331 | 0 | 401 | 513 | 2463 | 3589 |
| The Return Of The King | 183 | 2 | 268 | 510 | 2673 | 2459 |