knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(openintro)
library(dplyr)
library(ggplot2)

data('arbuthnot', package='openintro')
arbuthnot$boys #will only show the number of boys baptized each year
##  [1] 5218 4858 4422 4994 5158 5035 5106 4917 4703 5359 5366 5518 5470 5460 4793
## [16] 4107 4047 3768 3796 3363 3079 2890 3231 3220 3196 3441 3655 3668 3396 3157
## [31] 3209 3724 4748 5216 5411 6041 5114 4678 5616 6073 6506 6278 6449 6443 6073
## [46] 6113 6058 6552 6423 6568 6247 6548 6822 6909 7577 7575 7484 7575 7737 7487
## [61] 7604 7909 7662 7602 7676 6985 7263 7632 8062 8426 7911 7578 8102 8031 7765
## [76] 6113 8366 7952 8379 8239 7840 7640
#dollar sign basically says “go to the data frame that comes before me, and find the variable that comes after me”.

Exercise 1: What command would you use to extract just the counts of girls baptized?

arbuthnot$girls
##  [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.)

# Insert code for Exercise 2 here
ggplot(data = arbuthnot, aes(x = year, y = girls)) + 
  geom_line()

The overall trend from the late 1620s has been upward in the amount of girls baptized. There are many drops (especially around 1703-1704), however, if we look at the big picture, it has only been increasing.

Exercise 3: Now, generate a plot of the proportion of boys born over time. What do you see?

# Insert code for Exercise 3 here
arbuthnot <- arbuthnot %>%
  mutate(total = boys + girls)
ggplot(data = arbuthnot, aes(x = year, y = boys/total)) + geom_line()

#ggplot(data = arbuthnot, aes(x = year, y = boys)) + geom_line() 
#in case a proportion to total is not needed, then the code should be the above

Exercise 4:What years are included in this data set? What are the dimensions of the data frame? What are the variable (column) names?

# Insert code for Exercise 4 here
data('present', package='openintro')
range(present$year)
## [1] 1940 2002
#The years used in this dataset is from 1940 to 2002.
dim(present)
## [1] 63  3
#There are 63 rows and 3 columns in the arbuthnot dataset. 
colnames(present)
## [1] "year"  "boys"  "girls"
#The column names are "year", "boys", and "girls". 

Exercise 5: How do these counts compare to Arbuthnot’s? Are they of a similar magnitude?

# Insert code for Exercise 5 here
range(arbuthnot$year)
## [1] 1629 1710
#The range is from 1629 to 1710; a total of 81 years compared to the
#data in the present which looked at only 62 years.  '''
dim(arbuthnot)
## [1] 82  4
#There is a total of 82 rows and 4 columns in the arbuthnot dataset,compared to 63 rows and 3 columns in the present dataset.  
colnames(arbuthnot)
## [1] "year"  "boys"  "girls" "total"
#The column names are the same except the arbuthnot has a total column too. 

Exercise 6

Insert any text here.

# Insert code for Exercise 6 here
present <- present %>%
  mutate(total = boys + girls)
ggplot(data = present, aes(x = year, y = boys/total)) + geom_line()

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).

# Insert code for Exercise 7 here
present <- present %>%
        mutate(total = boys + girls)
present <- present %>%
        arrange(desc(total))
head(present)
## # A tibble: 6 × 4
##    year    boys   girls   total
##   <dbl>   <dbl>   <dbl>   <dbl>
## 1  1961 2186274 2082052 4268326
## 2  1960 2179708 2078142 4257850
## 3  1957 2179960 2074824 4254784
## 4  1959 2173638 2071158 4244796
## 5  1958 2152546 2051266 4203812
## 6  1962 2132466 2034896 4167362
#1961 had the most total number of births.

Further Questions: Arbuthnot's data showed boys and girls who are baptized from 1629 to 1710, but it would be interesting to see what the data is for unbaptized children.