library(tidyverse)
library(babynames)
AE 14: Debugging R code
Packages
We will use the following packages in this application exercise.
- tidyverse: For data import, wrangling, and visualization.
- babynames: For working with the Social Security Administration’s baby names data.
Popular baby names
The Social Security Administration maintains detailed historic records on every child born in the United States dating back to 1880.1 The data is published annually at the national and state-levels, and includes every name excluding those with fewer than 5 occurrences. The babynames package provides a convenient interface to this data. 2
1 Source: SSN
2 The package is no longer actively maintained. We will use a forked version of the package which has been submitted as a pending pull request.
The instructor has written code to analyze the popularity of baby names in the United States. Alas, the code is not working correctly. Your task is to debug the code and fix any issues. Pay attention to any errors, warnings, or messages generated by the code. It is possible for the code to run without any notification from R, but still produce incorrect output.
Write a function to show trends over time for specific name
<- function(person_name) {
name_trend |>
babynames filter(name == person_name) |>
ggplot(mapping = aes(x = year, y = n, color = sex)) +
geom_line() +
scale_color_brewer(type = "qual") +
labs(
title = str_glue(Name: {person_name}),
x = "Year",
y = "Number of births",
color = NULL
+
) theme_minimal()
}
name_trend("Benjamin")
Error in name_trend("Benjamin"): object 'Name' not found
Plot the total number of U.S. births over time using a stacked area chart
|>
applicants mutate(
sex = if_else(sex == "F", "Female", "Male"),
n_all = n_all / 1e06
|>
) ggplot(mapping = aes(x = year, y = n_all, fill = sex)) +
geom_line() +
scale_fill_brewer(type = "qual") +
labs(
title = "Total US births",
x = "Year",
y = "Millions",
fill = NULL,
caption = "Source: Social Security Administration"
+
) theme_minimal()
Compare naming trends to Disney princesses around film release years
# create data frame of disney princess films
<- tribble(
disney "princess", "film", "release_year",
"Snow White", "Snow White and the Seven Dwarfs", 1937,
"Cinderella", "Cinderella", 1950,
"Aurora", "Sleeping Beauty", 1959,
"Ariel", "The Little Mermaid", 1989,
"Belle", "Beauty and the Beast", 1991,
"Jasmine", "Aladdin", 1992,
"Pocahontas", "Pocahontas", 1995,
"Mulan", "Mulan", 1998,
"Tiana", "The Princess and the Frog", 2009,
"Rapunzel", "Tangled", 2010,
"Merida", "Brave", 2012,
"Elsa", "Frozen", 2013,
"Moana", "Moana", 2016
)
Error in `tribble()`:
! Must specify at least one column using the `~name` syntax.
# join together the data frames
|>
babynames # ignore men named after princesses - is this fair?
filter(sex == F) |>
inner_join(disney, by = c("name" = "princess")) |>
mutate(name = fct_reorder(.f = name, .x = release_year)) |>
# plot the trends over time, indicating release year
ggplot(mapping = aes(x = year, y = n)) +
facet_wrap(facets = vars(name, film), scales = "free_y", labeller = label_both()) +
geom_line() +
geom_vline(mapping = aes(xintercept = release_year), linetype = 2, alpha = .5) +
scale_x_continuous(breaks = c(1880, 1940, 2000)) +
theme_minimal() +
labs(title = "Popularity of Disney princess names",
x = "Year",
y = "Number of births")
Error in eval(expr, envir, enclos): object 'disney' not found
Write a function to show trends over time for the top N names in a specific year
<- function(n_year, n_rank = 5) {
top_n_trend # create lookup table
<- babynames |>
top_names group_by(name, sex) |>
summarize(count = as.numeric(sum(count))) |>
filter(count > 1000) |>
select(name, sex)
# filter babynames for top_names
<- babynames |>
filtered_names inner_join(top_names)
# get the top N names from n_year
<- filtered_names |>
top_names filter(year == n_year) |>
group_by(name, sex) |>
summarize(count = sum(count)) |>
group_by(sex) |>
mutate(rank = min_rank(desc(count))) |>
filter(rank <= n_rank) |>
arrange(sex, rank) |>
select(name, sex, rank)
# keep just the top N names over time and plot
|>
filtered_names inner_join(select(top_names, sex, name)) |>
ggplot(mapping = aes(x = year, y = count, color = name)) +
facet_wrap(facets = vars(sex), ncol = 1) +
geom_line() +
scale_color_brewer(type = "qual", palette = "Set3") +
labs(
title = str_glue("Most Popular Names of {n_year}"),
x = "Year",
y = "Number of births",
color = "Name"
+
) theme_minimal()
}
top_n_trend(n_year = 1986)
Error in `summarize()`:
ℹ In argument: `count = as.numeric(sum(count))`.
ℹ In group 1: `name = "Aaban"`, `sex = "M"`.
Caused by error in `sum()`:
! invalid 'type' (closure) of argument
top_n_trend(n_year = 2014)
Error in `summarize()`:
ℹ In argument: `count = as.numeric(sum(count))`.
ℹ In group 1: `name = "Aaban"`, `sex = "M"`.
Caused by error in `sum()`:
! invalid 'type' (closure) of argument
top_n_trend(n_year = 1986, n_rank = 10)
Error in `summarize()`:
ℹ In argument: `count = as.numeric(sum(count))`.
ℹ In group 1: `name = "Aaban"`, `sex = "M"`.
Caused by error in `sum()`:
! invalid 'type' (closure) of argument