library(tidyverse)
library(openintro)
Exercise 1 What command would you use to extract just the counts of
girls baptized? Try it!
## [1] 4683 4457 4102 4590 4839 4820 4928 4605 4457 4952 4784 5332 5200 4910 4617
## [16] 3997 3919 3395 3536 3181 2746 2722 2840 2908 2959 3179 3349 3382 3289 3013
## [31] 2781 3247 4107 4803 4881 5681 4858 4319 5322 5560 5829 5719 6061 6120 5822
## [46] 5738 5717 5847 6203 6033 6041 6299 6533 6744 7158 7127 7246 7119 7214 7101
## [61] 7167 7302 7392 7316 7483 6647 6713 7229 7767 7626 7452 7061 7514 7656 7683
## [76] 5738 7779 7417 7687 7623 7380 7288
Exercise 2 Is there an apparent trend in the number of girls
baptized over the years? How would you describe it? (To ensure that your
lab report is comprehensive, be sure to include the code needed to make
the plot as well as your written interpretation.)
The number of girl baptisms has a downward trend beginning in 1640,
and an upward trend beginning in 1660 and then evens out in the
1700s.
ggplot(data = arbuthnot, aes(x = year, y = girls)) +
geom_point()

Exercise 3 Now, generate a plot of the proportion of boys born over
time. What do you see?
arbuthnot <- arbuthnot %>%
mutate(total = boys + girls)
arbuthnot <- arbuthnot %>%
mutate(boy_to_girl_ratio = boys / girls)
arbuthnot <- arbuthnot %>%
mutate(boy_ratio = boys / total)
ggplot(data = arbuthnot, aes(x = year, y = boy_ratio)) +
geom_line()

arbuthnot <- arbuthnot %>%
mutate(total = boys + girls)
Exercise 4 What years are included in this data set? What are the
dimensions of the data frame? What are the variable (column) names?
1940-2002, 63 x 3, years, boys, girls.
## Rows: 63
## Columns: 3
## $ year <dbl> 1940, 1941, 1942, 1943, 1944, 1945, 1946, 1947, 1948, 1949, 1950…
## $ boys <dbl> 1211684, 1289734, 1444365, 1508959, 1435301, 1404587, 1691220, 1…
## $ girls <dbl> 1148715, 1223693, 1364631, 1427901, 1359499, 1330869, 1597452, 1…
Exercise 5 How do these counts compare to Arbuthnot’s? Are they of a
similar magnitude?
No. The mins and maxs of present are in the millions versus the
hundreds for arbuthnot.
arbuthnot %>%
summarize(min = min(boys), max = max(boys))
## # A tibble: 1 × 2
## min max
## <int> <int>
## 1 2890 8426
present %>%
summarize(min = min(boys), max = max(boys))
## # A tibble: 1 × 2
## min max
## <dbl> <dbl>
## 1 1211684 2186274
present[1,2] - arbuthnot[22,2]
## boys
## 1 1208794
Exercise 6 Make a plot that displays the proportion of boys born
over time. What do you see? Does Arbuthnot’s observation about boys
being born in greater proportion than girls hold up in the U.S.? Include
the plot in your response. Hint: You should be able to reuse your code
from Exercise 3 above, just replace the dataframe name.
The ratio of boys born moves closer to parity with girls between
1940-2002, but is still greater in every single year.
present <- present %>%
mutate(total = boys + girls)
present <- present %>%
mutate(boy_ratio = boys / total)
ggplot(data = present, aes(x = year, y = boy_ratio)) +
geom_line()

present <- present %>%
mutate(more_boys = boys > girls)
Exercise 7 In what year did we see the most total number of births
in the U.S.? Hint: First calculate the totals and save it as a new
variable. Then, sort your dataset in descending order based on the total
column. You can do this interactively in the data viewer by clicking on
the arrows next to the variable names. To include the sorted result in
your report you will need to use two new functions: arrange (for
sorting). We can arrange the data in a descending order with another
function: desc (for descending order). The sample code is provided
below.
present %>%
arrange(desc(total))
## # A tibble: 63 × 6
## year boys girls total boy_ratio more_boys
## <dbl> <dbl> <dbl> <dbl> <dbl> <lgl>
## 1 1961 2186274 2082052 4268326 0.512 TRUE
## 2 1960 2179708 2078142 4257850 0.512 TRUE
## 3 1957 2179960 2074824 4254784 0.512 TRUE
## 4 1959 2173638 2071158 4244796 0.512 TRUE
## 5 1958 2152546 2051266 4203812 0.512 TRUE
## 6 1962 2132466 2034896 4167362 0.512 TRUE
## 7 1956 2133588 2029502 4163090 0.513 TRUE
## 8 1990 2129495 2028717 4158212 0.512 TRUE
## 9 1991 2101518 2009389 4110907 0.511 TRUE
## 10 1963 2101632 1996388 4098020 0.513 TRUE
## # ℹ 53 more rows
LS0tCnRpdGxlOiAiTGFiIDE6IEludHJvIHRvIFIiCmF1dGhvcjogIkFrZWVtIExhd3JlbmNlIgpkYXRlOiAiYERlY2V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