library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(openintro)
## Loading required package: airports
## Loading required package: cherryblossom
## Loading required package: usdata
arbuthnot
## # A tibble: 82 × 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
## # ℹ 72 more rows
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
ggplot(data = arbuthnot, aes(x = year, y = girls)) +
geom_point()
ggplot(data = arbuthnot, aes(x = year, y = girls)) +
geom_line()
With the help of geom_line, we are able to notice that the trend started by the year 1660, and it just kept increasing.
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
We are interested in using this new vector of the total number of baptisms to generate some plots, so we’ll want to save it as a permanent column in our data frame. We can do this using the following code:
arbuthnot <- arbuthnot %>%
mutate(total = boys + girls)
This code has a lot of new pieces to it, so let’s break it down. In the first line we are doing two things, (1) adding a new total column to this updated data frame, and (2) overwriting the existing arbutnot data frame with an updated data frame that includes the new total column. We are able to chain these two processes together using the piping (%>%) operator. The piping operator takes the output of the previous expression and “pipes it” into the first argument of the next expression.
To continue our analogy with mathematical functions, x %>% f(y) is equivalent to f(x, y). Connecting arbuthnot and mutate(total = boys + girls) with the pipe operator is the same as typing mutate(arbuthnot, total = boys + girls), where arbuthnot becomes the first argument included in the mutate() function.
ggplot(data = arbuthnot, aes(x = year, y = total)) +
geom_line()
Alternatively, you could calculate this ratio for every year by acting on the complete boys and girls columns, and then save those calculations into a new variable named boy_to_girl_ratio:
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()
I see that the proportion of boys skyrocketed in the year 1660
example, you can create a new variable called more_boys that tells us whether the number of births of boys outnumbered that of girls in each year with the following code:
arbuthnot <- arbuthnot %>%
mutate(more_boys = boys > girls)
present
## # A tibble: 63 × 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
## # ℹ 53 more rows
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:
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
the years ranges from 1940 to 2002, its dimensions are 63x3, and the variable names are year, boys, and girls.
No they are not since the counts for the present data set is in the millions while Arbuthnot’s ranges in the thousands
present <- present %>%
mutate(Total= boys + girls)
present <- present %>%
mutate(boys_ratio= boys/ Total)
ggplot(data= present, aes(x= year, y= boys_ratio))+
geom_line()
It is deacreasing over time. Arbuthnot’s observation about boys being born in greater proportion than girls doesn’t hold up in the US.
present %>%
arrange(desc(Total))
## # A tibble: 63 × 5
## year boys girls Total boys_ratio
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1961 2186274 2082052 4268326 0.512
## 2 1960 2179708 2078142 4257850 0.512
## 3 1957 2179960 2074824 4254784 0.512
## 4 1959 2173638 2071158 4244796 0.512
## 5 1958 2152546 2051266 4203812 0.512
## 6 1962 2132466 2034896 4167362 0.512
## 7 1956 2133588 2029502 4163090 0.513
## 8 1990 2129495 2028717 4158212 0.512
## 9 1991 2101518 2009389 4110907 0.511
## 10 1963 2101632 1996388 4098020 0.513
## # ℹ 53 more rows
It was in 1940 where the Total equaled to 2,360,399