##Loading packages and libraries
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.2.3
## Warning: package 'ggplot2' was built under R version 4.2.3
## Warning: package 'tibble' was built under R version 4.2.3
## Warning: package 'tidyr' was built under R version 4.2.3
## Warning: package 'readr' was built under R version 4.2.3
## Warning: package 'purrr' was built under R version 4.2.3
## Warning: package 'dplyr' was built under R version 4.2.3
## Warning: package 'stringr' was built under R version 4.2.3
## Warning: package 'forcats' was built under R version 4.2.3
## Warning: package 'lubridate' was built under R version 4.2.3
## ── 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.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ 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)
## Warning: package 'openintro' was built under R version 4.2.3
## Loading required package: airports
## Loading required package: cherryblossom
## Loading required package: usdata
library(dplyr)
library(magrittr)
##
## Attaching package: 'magrittr'
##
## The following object is masked from 'package:purrr':
##
## set_names
##
## The following object is masked from 'package:tidyr':
##
## extract
##Data set
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
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…
##Some exploration
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 just the counts of girls baptized? Try it!
ggplot(data = arbuthnot, aes(x = year, y = girls)) +
geom_point()
##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.)
ggplot(data = arbuthnot, aes(x = year, y = girls)) +
geom_line()
There is increase of the number of girls baptized.
##Exercise 3 Now, generate a plot of the proportion of boys born over time. What do you see?
ggplot(data = arbuthnot, aes(x = year, y = boys)) + geom_line() + geom_point()
rbuthnot <- arbuthnot %>%
mutate(more_boys = boys > girls)
##Exercise 4 What years are included in this data set? What are the dimensions of the data frame? What are the variable (column) names?
df <-
unique(present$year)
NROW(present); NCOL(present)
## [1] 63
## [1] 3
##Exercise 5 How do these counts compare to Arbuthnot’s? Are they of a similar magnitude?
present <- present %>%
mutate(total = boys + girls)
head(present)
## # A tibble: 6 × 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
##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 dataframe name.
present %>% ggplot(aes(x= year)) +
geom_line(aes(y = boys, color = "Boys")) +
geom_line(aes(y = girls, color = "Girls")) +
scale_color_manual(values = c("Boys" = "Blue", "Girls" = "Purple")) +
labs(y = "Number of Births in the US")
There is a drop in the birthratein both. The proportion of boys born is
slightly higher than girls over the time.
##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). The sample code is provided below.
present <- present %>%
mutate(total = boys + girls)
present <- present %>%
arrange(desc(total))
head(present, 3)
## # A tibble: 3 × 4
## year boys girls total
## <dbl> <dbl> <dbl> <dbl>
## 1 1961 2186274 2082052 4268326
## 2 1960 2179708 2078142 4257850
## 3 1957 2179960 2074824 4254784