we will run libraries before we answer any exercise questions.
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── 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
The Arbuthnot data set refers to the work of Dr. John Arbuthnot, an 18th century physician, writer, and mathematician. He was interested in the ratio of newborn boys to newborn girls, so he gathered the baptism records for children born in London for every year from 1629 to 1710. Once again, we can view the data by typing its name into the console.
Read the data set and re read with a better function.
# load the data set from the package
data('arbuthnot', package = 'openintro')
glimpse(arbuthnot)
## Rows: 82
## Columns: 3
## $ year <int> 1629, 1630, 1631, 1632, 1633, 1634, 1635, 1636, 1637, 1638, 1639…
## $ boys <int> 5218, 4858, 4422, 4994, 5158, 5035, 5106, 4917, 4703, 5359, 5366…
## $ girls <int> 4683, 4457, 4102, 4590, 4839, 4820, 4928, 4605, 4457, 4952, 4784…
The command below would show you all the girl 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
The line graph shows the increase of girls baptized over time starting around 1600 to 1700 and up.
arbuthnot %>%
ggplot(mapping=aes(x = year, y=girls))+
geom_line(color = "grey") +
geom_point(color = 'black')
# Create the total amounts of boys and girls
arbuthnot <- arbuthnot %>%
mutate(total = boys + girls)
# Proportion of boys over girls
arbuthnot <- arbuthnot %>% mutate(boy_ratio = boys / total)
# Draw the line graph to show the evolution of the proportion
#of boys over time
arbuthnot %>% ggplot(aes(x=year, y = boy_ratio)) +
geom_line()
We notice that the proportion of boys is decreasing over times.
The results shows only the years included in the data set < present > . Based on our observation, the data set has 63 rows and 3 variables. See result below:
# load the dataset
data('present',package = 'openintro')
# shows only the years included in the data set
### present %>% distinct(year)
unique(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
# show dimension and table name or variables
str(present)
## tibble [63 × 3] (S3: tbl_df/tbl/data.frame)
## $ year : num [1:63] 1940 1941 1942 1943 1944 ...
## $ boys : num [1:63] 1211684 1289734 1444365 1508959 1435301 ...
## $ girls: num [1:63] 1148715 1223693 1364631 1427901 1359499 ...
The present variables for both boys and girls hold bigger numbers than the ones arbuthnot data set
# let create total variable for presents and compare the numbers to Arbuthnot
present <- present %>%
mutate(total = boys + girls)
# see 7 rows
head(present,7)
## # A tibble: 7 × 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
# Proportion of boys over girls
present <- present %>% mutate(boy_ratio = boys / total)
# Draw the line graph to show the evolution of the proportion
#of boys over time
present %>% ggplot(aes(x=year, y = boy_ratio)) +
geom_line()
The variable total was created in exercise 5.
1961 is the year with the highest number of births in the US at 4268326.
present %>% arrange(desc(total))
## # A tibble: 63 × 5
## year boys girls total boy_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
present %>% select(year, total) %>% filter(total == max(total))
## # A tibble: 1 × 2
## year total
## <dbl> <dbl>
## 1 1961 4268326