First Code Exercise

# install.packages("tidyverse")
# install.packages("openintro")
library(tidyverse)
## -- Attaching packages -------------------------------------------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.3     v dplyr   1.0.2
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## -- Conflicts ----------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(openintro)
## Loading required package: airports
## Loading required package: cherryblossom
## Loading required package: usdata

dr. arbuthnot’s baptism records

arbuthnot
## # A tibble: 82 x 3
##     year  boys girls
##    <int> <int> <int>
##  1  1629  5218  4683
##  2  1630  4858  4457
##  3  1631  4422  4102
##  4  1632  4994  4590
##  5  1633  5158  4839
##  6  1634  5035  4820
##  7  1635  5106  4928
##  8  1636  4917  4605
##  9  1637  4703  4457
## 10  1638  5359  4952
## # ... with 72 more rows
glimpse(arbuthnot)
## Rows: 82
## Columns: 3
## $ year  <int> 1629, 1630, 1631, 1632, 1633, 1634, 1635, 1636, 1637, 1638, 1...
## $ boys  <int> 5218, 4858, 4422, 4994, 5158, 5035, 5106, 4917, 4703, 5359, 5...
## $ girls <int> 4683, 4457, 4102, 4590, 4839, 4820, 4928, 4605, 4457, 4952, 4...
arbuthnot$boys
##  [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

Exercise 1

What command would you use to extract the counts of girls baptized?

arbuthnot$girls

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

Data Visualization

ggplot(data = arbuthnot, aes(x = year, y = girls)) + 
  geom_point()

ggplot(data = arbuthnot, aes(x = year, y = girls)) +
  geom_line()

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

Though for the first 30 years there was a trend down, the overall trend still shows an increase in the amount of baptisms of girls.

The code needed is:

ggplot(data = arbuthnot, aes(x = year, y = girls)) + geom_line()

?ggplot
## starting httpd help server ... done

R as a big calculator

5218 + 4683
## [1] 9901
arbuthnot$boys + arbuthnot$girls
##  [1]  9901  9315  8524  9584  9997  9855 10034  9522  9160 10311 10150 10850
## [13] 10670 10370  9410  8104  7966  7163  7332  6544  5825  5612  6071  6128
## [25]  6155  6620  7004  7050  6685  6170  5990  6971  8855 10019 10292 11722
## [37]  9972  8997 10938 11633 12335 11997 12510 12563 11895 11851 11775 12399
## [49] 12626 12601 12288 12847 13355 13653 14735 14702 14730 14694 14951 14588
## [61] 14771 15211 15054 14918 15159 13632 13976 14861 15829 16052 15363 14639
## [73] 15616 15687 15448 11851 16145 15369 16066 15862 15220 14928

Adding a new variable to the data frame

arbuthnot <- arbuthnot %>%
  mutate(total = boys + girls)
ggplot(data = arbuthnot, aes(x = year, y = total)) + 
  geom_line()

5218/4683
## [1] 1.114243
arbuthnot <- arbuthnot %>%
  mutate(boy_to_girl_ratio = boys / girls)
5218 / (5218 + 4683)
## [1] 0.5270175
arbuthnot <- arbuthnot %>%
  mutate(boy_ratio = boys / total)

Exercise 3

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

The proportion has fluctuated a lot up and down from year to year. There is not much of a trend displayed in the graph.

ggplot(data = arbuthnot, aes(x = year, y = boys / total )) + 
  geom_line()

arbuthnot <- arbuthnot %>%
  mutate(more_boys = boys > girls)

More Practice

arbuthnot %>%
  summarize(min = min(boys),
            max = max(boys)
            )
## # A tibble: 1 x 2
##     min   max
##   <int> <int>
## 1  2890  8426

Exercise 4

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

the years are from 1940-2002. This dataset is 3 by 63 and the column names are year, boys and girls.

present
## # A tibble: 63 x 3
##     year    boys   girls
##    <dbl>   <dbl>   <dbl>
##  1  1940 1211684 1148715
##  2  1941 1289734 1223693
##  3  1942 1444365 1364631
##  4  1943 1508959 1427901
##  5  1944 1435301 1359499
##  6  1945 1404587 1330869
##  7  1946 1691220 1597452
##  8  1947 1899876 1800064
##  9  1948 1813852 1721216
## 10  1949 1826352 1733177
## # ... with 53 more rows

Exercise 5

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

These counts are much much larger magnitude, with numbers getting into 7 figures while Arbuthnot’s count was much smaller, at only about the thousands.

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 name of the data frame.

The overall proportion of boys who are born is going down in the present. His prediction was false as the proportion of girls being born is increasing based on the two plots below

present <- present %>%
  mutate(total = boys + girls)
ggplot(data = present , aes(x = year, y = boys / total )) + 
  geom_line()

ggplot(data = present , aes(x = year, y = girls / total )) + 
  geom_line()

Exercise 7

In what year did we see the most total number of births in the U.S.?

The most total births were in 1961

present
## # A tibble: 63 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
##  7  1946 1691220 1597452 3288672
##  8  1947 1899876 1800064 3699940
##  9  1948 1813852 1721216 3535068
## 10  1949 1826352 1733177 3559529
## # ... with 53 more rows
present %>%
  arrange(desc(total))
## # A tibble: 63 x 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
##  7  1956 2133588 2029502 4163090
##  8  1990 2129495 2028717 4158212
##  9  1991 2101518 2009389 4110907
## 10  1963 2101632 1996388 4098020
## # ... with 53 more rows