# install.packages("tidyverse")
# install.packages("openintro")
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.1.3
## -- Attaching packages --------------------------------------- tidyverse 1.3.2 --
## v ggplot2 3.3.6 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.9
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## Warning: package 'ggplot2' was built under R version 4.1.3
## Warning: package 'tidyr' was built under R version 4.1.3
## Warning: package 'readr' was built under R version 4.1.3
## Warning: package 'dplyr' was built under R version 4.1.3
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(openintro)
## Warning: package 'openintro' was built under R version 4.1.3
## Loading required package: airports
## Loading required package: cherryblossom
## Loading required package: usdata
knitr::include_graphics("baby.png")
Load and view the data.
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
## # i Use `print(n = ...)` to see more rows
This command will only show the number of boys baptized each year.
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
What command would you use to extract just the counts of girls baptized? Try it!
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
R has some powerful functions for making graphics. We can create a simple plot of the number of girls baptized per year with the command
ggplot(data = arbuthnot, aes(x = year, y = girls)) +
geom_point()
For instance, if you wanted to visualize the above plot using a line graph, you would replace geom_point() with geom_line().
ggplot(data = arbuthnot, aes(x = year, y = girls)) +
geom_line()
Is there an apparent trend in the number of girls baptized over the years? How would you describe it?
Answer: From about 1660 on, there is generally an upward trend in the number of girls baptized.
To see the total number of baptisms in 1629. We could repeat this once for each year, but there is a faster way. If we add the vector for baptisms for boys and girls, R will compute all sums simultaneously.
#we can make a plot of the total number of baptisms per year with the command
plot(arbuthnot$year, arbuthnot$boys + arbuthnot$girls, type = "l")
# The proportion of newborns that are boys
5218 / (5218 + 4683)
## [1] 0.5270175
# or
arbuthnot$boys / (arbuthnot$boys + arbuthnot$girls)
## [1] 0.5270175 0.5215244 0.5187705 0.5210768 0.5159548 0.5109082 0.5088698
## [8] 0.5163831 0.5134279 0.5197362 0.5286700 0.5085714 0.5126523 0.5265188
## [15] 0.5093518 0.5067868 0.5080341 0.5260366 0.5177305 0.5139059 0.5285837
## [22] 0.5149679 0.5322023 0.5254569 0.5192526 0.5197885 0.5218447 0.5202837
## [29] 0.5080030 0.5116694 0.5357262 0.5342132 0.5361942 0.5206108 0.5257482
## [36] 0.5153557 0.5128359 0.5199511 0.5134394 0.5220493 0.5274422 0.5232975
## [43] 0.5155076 0.5128552 0.5105507 0.5158214 0.5144798 0.5284297 0.5087122
## [50] 0.5212285 0.5083822 0.5096910 0.5108199 0.5060426 0.5142178 0.5152360
## [57] 0.5080788 0.5155165 0.5174905 0.5132301 0.5147925 0.5199527 0.5089677
## [64] 0.5095857 0.5063659 0.5123973 0.5196766 0.5135590 0.5093183 0.5249190
## [71] 0.5149385 0.5176583 0.5188268 0.5119526 0.5026541 0.5158214 0.5181790
## [78] 0.5174052 0.5215362 0.5194175 0.5151117 0.5117899
The command “mutate” creates an additional variable, “total”, to our dataset (it was originally year, boys, and girsls)
arbuthnot <- arbuthnot %>%
mutate(total = boys + girls)
You can make a line plot of the total number of baptisms per year with the command
ggplot(data = arbuthnot, aes(x = year, y = total)) +
geom_line()
Similarly to how we computed the total number of births, you can compute the ratio of the number of boys to the number of girls baptized in 1629 with just the actual numbers,
or
You could create a 5th variable by the “mutate” command again, “boy_to_girl_ratio”.
5218 / 4683
## [1] 1.114243
# or
arbuthnot <- arbuthnot %>%
mutate(boy_to_girl_ratio = boys / girls)
You can also compute the proportion of newborns that are boys for all years:
arbuthnot <- arbuthnot %>%
mutate(boy_ratio = boys / total)
Now, make a plot of the proportion of boys over time. What do you see?
ggplot(data = arbuthnot, aes(x = year, y = boys/total)) +
geom_line()
# This plot does not seem to show any general change in trends over time except a slight increase in the proportion of boys in 1660.
In addition to simple mathematical operators like subtraction and division, you can ask R to make comparisons like greater than, >, less than, <, and equality, ==. For example, we can ask if the number of births of boys outnumber that of girls in each year with the expression
arbuthnot <- arbuthnot %>%
mutate(more_boys = boys > girls)
# There were greater proportion of boys than girls every year.
To find the minimum and maximum values of columns, you can use the functions min and max within a summarize() call, which you will learn more about in the following lab. Here’s an example of how to find the minimum and maximum amount of boy births in a year:
present %>%
summarize(min = min(boys), max = max(boys))
## # A tibble: 1 x 2
## min max
## <dbl> <dbl>
## 1 1211684 2186274
present$year
## [1] 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954
## [16] 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969
## [31] 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984
## [46] 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
## [61] 2000 2001 2002
# The output was between 1940 and 2002
dim(present)
## [1] 63 3
# There are 62 years with the same three variables; year, boys and girls
names(present)
## [1] "year" "boys" "girls"
Answer: The counts for Arbuthnot’s are 5-10 thousand, whereas the counts for Present are over 1 million.
Make a plot that displays the boy-to-girl ratio for every year in the data set. 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.
ggplot(data = present, aes(x=year, y=boys/girls)) +
geom_line()
# The ratio is decreasing over the years. Arbuthnot's observation about boys being born in greater proportion than girls does not hold up in the U.S.
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 %>%
mutate(total = boys+girls) %>%
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
## # i Use `print(n = ...)` to see more rows
# We saw the most births in 1961.