library(tidyverse)
library(openintro)
Exercise 1
## [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
ggplot(data = arbuthnot, aes(x = year, y = girls)) +
geom_point()

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 plot is showing an increase in the number of girls being baptized from 1629 to 1710.
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()

It looks like the proportion of boys is slowely dropping.
Exercise 4
What years are included in this data set? What are the dimensions of the data frame? What are the variable (column) names?
df <-
unique(present$year)
NROW(present); NCOL(present)
## [1] 63
## [1] 3
The data set include years 1940 to 2002 and contains 63 rows and 3 columns (year, boys, girls)
Exercise 5
How do these counts compare to Arbuthnot’s? Are they of a similar magnitude?
present <- present %>%
mutate(total = boys + girls)
head(present)
## # A tibble: 6 x 4
## year boys girls total
## <dbl> <dbl> <dbl> <dbl>
## 1 1940 1211684 1148715 2360399
## 2 1941 1289734 1223693 2513427
## 3 1942 1444365 1364631 2808996
## 4 1943 1508959 1427901 2936860
## 5 1944 1435301 1359499 2794800
## 6 1945 1404587 1330869 2735456
The values in present are much bigger than arbuthnot by 3 orders of magnitude
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.
present <- present %>%
mutate(total = boys + girls)
present <- present %>%
mutate(boy_to_girl_ratio = boys / girls)
present <- present %>%
mutate(boy_ratio = boys / total)
ggplot(data = present, aes(x = year, y = boy_ratio)) +
geom_line()

The graph is very different from Arbuthnot’s observation, the boy_ratio is decreasing with time.
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.
# Insert code for Exercise 7 here
present[which.max(present$total), ]
## # A tibble: 1 x 6
## year boys girls total boy_to_girl_ratio boy_ratio
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1961 2186274 2082052 4268326 1.05 0.512
It looks like year 1961 had the most total number of births in the U.S at 4268326.
LS0tCnRpdGxlOiAiTGFiIDE6IEludHJvIHRvIFIiCmF1dGhvcjogIktlbmFuIFNvb2tsYWxsIgpkYXRlOiAiYHIgU3lzLkRhdGUoKWAiCm91dHB1dDogb3BlbmludHJvOjpsYWJfcmVwb3J0Ci0tLQoKYGBge3IgbG9hZC1wYWNrYWdlcywgbWVzc2FnZT1GQUxTRX0KbGlicmFyeSh0aWR5dmVyc2UpCmxpYnJhcnkob3BlbmludHJvKQpgYGAKCiMjIyBFeGVyY2lzZSAxCgpgYGB7ciB2aWV3LWdpcmxzLWNvdW50c30KYXJidXRobm90JGdpcmxzCmBgYAoKCiMjIyBFeGVyY2lzZSAyCgpgYGB7ciB0cmVuZC1naXJsc30KZ2dwbG90KGRhdGEgPSBhcmJ1dGhub3QsIGFlcyh4ID0geWVhciwgeSA9IGdpcmxzKSkgKyAKICBnZW9tX3BvaW50KCkKYGBgCgpJcyB0a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