library(tidyverse)## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5 ✓ purrr 0.3.4
## ✓ tibble 3.1.3 ✓ dplyr 1.0.7
## ✓ tidyr 1.1.3 ✓ stringr 1.4.0
## ✓ readr 2.0.0 ✓ forcats 0.5.1
## ── 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
library(dplyr)Explore the data and plot various elements of the raw data set. Look at the requested plot and data for girls baptism.
# {r view-all-counts]}
data('arbuthnot', package='openintro')
#Look at the dataset dimensions, class and a bit of dataset.
dim(arbuthnot)## [1] 82 3
class(arbuthnot)## [1] "tbl_df" "tbl" "data.frame"
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…
#Look at the dataset present for later exercise and join table
glimpse(present)## Rows: 63
## Columns: 3
## $ year <dbl> 1940, 1941, 1942, 1943, 1944, 1945, 1946, 1947, 1948, 1949, 1950…
## $ boys <dbl> 1211684, 1289734, 1444365, 1508959, 1435301, 1404587, 1691220, 1…
## $ girls <dbl> 1148715, 1223693, 1364631, 1427901, 1359499, 1330869, 1597452, 1…
dim(present)## [1] 63 3
one <- arbuthnot[3:82, ]
two <- present [3:63, ]
# You can supply data frames as arguments:
combined<-bind_rows(one, two)
glimpse(combined)## Rows: 141
## Columns: 3
## $ year <dbl> 1631, 1632, 1633, 1634, 1635, 1636, 1637, 1638, 1639, 1640, 1641…
## $ boys <dbl> 4422, 4994, 5158, 5035, 5106, 4917, 4703, 5359, 5366, 5518, 5470…
## $ girls <dbl> 4102, 4590, 4839, 4820, 4928, 4605, 4457, 4952, 4784, 5332, 5200…
tail(combined)## # A tibble: 6 × 3
## year boys girls
## <dbl> <dbl> <dbl>
## 1 1997 1985596 1895298
## 2 1998 2016205 1925348
## 3 1999 2026854 1932563
## 4 2000 2076969 1981845
## 5 2001 2057922 1968011
## 6 2002 2057979 1963747
#full view to check what the full file looks like when combined
view(combined)
# {Look at only the view-girls-counts or number of girls baptised}
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
# Graph the girls vs time as points or a line
ggplot(data = arbuthnot, aes(x = year, y = girls)) +
geom_point()ggplot(data = arbuthnot, aes(x = year, y = girls)) +
geom_line()#{r view-boys-counts or number baptised and plot as point graph the boys baptised.
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
ggplot(data = arbuthnot, aes(x = year, y = boys)) +
geom_point()# Plot the total boys and girls baptised.
ggplot(data = arbuthnot, aes(x = year, y = girls+boys)) +
geom_point()Key trends in the girls baptised: 1) Girls rate is always less than Boys rate. 2) Suggests that sex ratio in births is always more boys than girls (assuming baptisms are representative birth rate proxy) 3) Girls and Boys total baptism numbers are changing but the ratio trend remains similar with more boys than girls through all of these variations. 4) Variation in the total baptisms (up and down) is not defined with a cause in the data but we could hypothsize that it is related to other factors such as famine, disease or similar. But it could be other factures such as lack of data or changes in where people lived so that the dip for a period of time was driven by other factors ( English expansion or immigration)
Insert any text here.
# Insert code for Exercise 2 here
arbuthnot <- arbuthnot %>%
mutate(total = boys + girls)
glimpse(arbuthnot)## Rows: 82
## Columns: 4
## $ 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…
## $ total <int> 9901, 9315, 8524, 9584, 9997, 9855, 10034, 9522, 9160, 10311, 10…
#Plot the total bapthisms by year
ggplot(data = arbuthnot, aes(x = year, y = total)) +
geom_line()arbuthnot <- arbuthnot %>%
mutate(boy_to_girl_ratio = boys / girls)
arbuthnot <- arbuthnot %>%
mutate(boy_ratio = boys / total)
glimpse(arbuthnot)## Rows: 82
## Columns: 6
## $ year <int> 1629, 1630, 1631, 1632, 1633, 1634, 1635, 1636, 1637…
## $ boys <int> 5218, 4858, 4422, 4994, 5158, 5035, 5106, 4917, 4703…
## $ girls <int> 4683, 4457, 4102, 4590, 4839, 4820, 4928, 4605, 4457…
## $ total <int> 9901, 9315, 8524, 9584, 9997, 9855, 10034, 9522, 916…
## $ boy_to_girl_ratio <dbl> 1.114243, 1.089971, 1.078011, 1.088017, 1.065923, 1.…
## $ boy_ratio <dbl> 0.5270175, 0.5215244, 0.5187705, 0.5210768, 0.515954…
arbuthnot <- arbuthnot %>%
mutate(girls_to_boy_ratio = girls/boys)
arbuthnot <- arbuthnot %>%
mutate(girls_ratio = girls / total)
ggplot(data = arbuthnot, aes(x = year, y = girls)) +
geom_line()ggplot(data=arbuthnot, aes(x=year, y=girls_ratio))+geom_point()summary(arbuthnot)## year boys girls total boy_to_girl_ratio
## Min. :1629 Min. :2890 Min. :2722 Min. : 5612 Min. :1.011
## 1st Qu.:1649 1st Qu.:4759 1st Qu.:4457 1st Qu.: 9199 1st Qu.:1.048
## Median :1670 Median :6073 Median :5718 Median :11813 Median :1.065
## Mean :1670 Mean :5907 Mean :5535 Mean :11442 Mean :1.071
## 3rd Qu.:1690 3rd Qu.:7576 3rd Qu.:7150 3rd Qu.:14723 3rd Qu.:1.088
## Max. :1710 Max. :8426 Max. :7779 Max. :16145 Max. :1.156
## boy_ratio girls_to_boy_ratio girls_ratio
## Min. :0.5027 Min. :0.8650 Min. :0.4638
## 1st Qu.:0.5118 1st Qu.:0.9195 1st Qu.:0.4790
## Median :0.5157 Median :0.9392 Median :0.4843
## Mean :0.5170 Mean :0.9347 Mean :0.4830
## 3rd Qu.:0.5210 3rd Qu.:0.9538 3rd Qu.:0.4882
## Max. :0.5362 Max. :0.9894 Max. :0.4973
Evaluate how often there are more boys born than girls.
View indicates 100% of the years has more boys baptised than girls ( assuming this is a birth rate proxy)
# Insert code for Exercise 3 here
arbuthnot <- arbuthnot %>%
mutate(total = boys + girls)
arbuthnot <- arbuthnot %>%
mutate(more_boys = boys > girls)
view(arbuthnot)As seen in the view or by looking at glimpse/ tail: The view for present runs from years 1940 to 2002. Dimensions provides that data set Present is 3 columns X 63 rows in dimension prior to adding additional columns. The column names are listed in the summary view and are: year, boys and girls (also listed in the variable names request)
# Insert code for Exercise 4 here
data('present', package='openintro')
#Look at dataset start and end data. Ensure it is arranged by year and then look at start and end of dataset.
#Also look at varible names, dimension and summary statistics
present<-arrange(present,year)
glimpse(present)## Rows: 63
## Columns: 3
## $ year <dbl> 1940, 1941, 1942, 1943, 1944, 1945, 1946, 1947, 1948, 1949, 1950…
## $ boys <dbl> 1211684, 1289734, 1444365, 1508959, 1435301, 1404587, 1691220, 1…
## $ girls <dbl> 1148715, 1223693, 1364631, 1427901, 1359499, 1330869, 1597452, 1…
tail(present)## # A tibble: 6 × 3
## year boys girls
## <dbl> <dbl> <dbl>
## 1 1997 1985596 1895298
## 2 1998 2016205 1925348
## 3 1999 2026854 1932563
## 4 2000 2076969 1981845
## 5 2001 2057922 1968011
## 6 2002 2057979 1963747
variable.names(present)## [1] "year" "boys" "girls"
dim(present)## [1] 63 3
summary(present)## year boys girls
## Min. :1940 Min. :1211684 Min. :1148715
## 1st Qu.:1956 1st Qu.:1799857 1st Qu.:1711404
## Median :1971 Median :1924868 Median :1831679
## Mean :1971 Mean :1885600 Mean :1793915
## 3rd Qu.:1986 3rd Qu.:2058524 3rd Qu.:1965538
## Max. :2002 Max. :2186274 Max. :2082052
# worst case you could look at it but may be a bad idea if large dataset
view(present)The combined file can be used to plot the total (girls+boys) to view the difference in magnitude of the datasets directly. Additionally we can look at the two summary views below and see the Arbuthnot avg of the total is 11,442 and the US average in the present file of the total is 3,679,515. Additionally we see the male birth rate ratio is nearly the same on average when comparing the historic data and the more recent data (historic Arbuthnot male ratio is 0.5170 and the present male ratio is 0.5125)
# Insert code for Exercise 5 here
one <- arbuthnot[3:82, ]
two <- present [5:63, ]
# You can supply data frames as arguments:
combined<-bind_rows(one, two)
arbuthnot <- arbuthnot %>%
mutate(total = boys + girls)
arbuthnot <- arbuthnot %>%
mutate(boy_ratio = boys / total)
present <- present %>%
mutate(total = boys + girls)
present<-present %>%
mutate(boys_ratio_present=boys/total)
glimpse(present)## Rows: 63
## Columns: 5
## $ year <dbl> 1940, 1941, 1942, 1943, 1944, 1945, 1946, 1947, 194…
## $ boys <dbl> 1211684, 1289734, 1444365, 1508959, 1435301, 140458…
## $ girls <dbl> 1148715, 1223693, 1364631, 1427901, 1359499, 133086…
## $ total <dbl> 2360399, 2513427, 2808996, 2936860, 2794800, 273545…
## $ boys_ratio_present <dbl> 0.5133386, 0.5131376, 0.5141926, 0.5138001, 0.51356…
# Options to evaluate - one is to look at the two plots seperate of the total counts or we could add the boys and girls to allow seeing each of the data sets ploted seperately. The alternative is to use the combined table and look at the plot over time to directly on one plot see the difference.
ggplot(data=present, aes(x=year, y=total))+geom_point()ggplot(data=arbuthnot, aes(x=year, y=total))+geom_point()ggplot(data=combined, aes(x=year,y=total))+geom_point()## Warning: Removed 59 rows containing missing values (geom_point).
# Look at the average of the total(boys+girls) for Arbuthnot and for the Present file.
summary(arbuthnot)## year boys girls total boy_to_girl_ratio
## Min. :1629 Min. :2890 Min. :2722 Min. : 5612 Min. :1.011
## 1st Qu.:1649 1st Qu.:4759 1st Qu.:4457 1st Qu.: 9199 1st Qu.:1.048
## Median :1670 Median :6073 Median :5718 Median :11813 Median :1.065
## Mean :1670 Mean :5907 Mean :5535 Mean :11442 Mean :1.071
## 3rd Qu.:1690 3rd Qu.:7576 3rd Qu.:7150 3rd Qu.:14723 3rd Qu.:1.088
## Max. :1710 Max. :8426 Max. :7779 Max. :16145 Max. :1.156
## boy_ratio girls_to_boy_ratio girls_ratio more_boys
## Min. :0.5027 Min. :0.8650 Min. :0.4638 Mode:logical
## 1st Qu.:0.5118 1st Qu.:0.9195 1st Qu.:0.4790 TRUE:82
## Median :0.5157 Median :0.9392 Median :0.4843
## Mean :0.5170 Mean :0.9347 Mean :0.4830
## 3rd Qu.:0.5210 3rd Qu.:0.9538 3rd Qu.:0.4882
## Max. :0.5362 Max. :0.9894 Max. :0.4973
summary(present)## year boys girls total
## Min. :1940 Min. :1211684 Min. :1148715 Min. :2360399
## 1st Qu.:1956 1st Qu.:1799857 1st Qu.:1711404 1st Qu.:3511262
## Median :1971 Median :1924868 Median :1831679 Median :3756547
## Mean :1971 Mean :1885600 Mean :1793915 Mean :3679515
## 3rd Qu.:1986 3rd Qu.:2058524 3rd Qu.:1965538 3rd Qu.:4023830
## Max. :2002 Max. :2186274 Max. :2082052 Max. :4268326
## boys_ratio_present
## Min. :0.5112
## 1st Qu.:0.5121
## Median :0.5125
## Mean :0.5125
## 3rd Qu.:0.5130
## Max. :0.5143
Similar to the boys ratio seen in Arbuthnot we see the boy_ratio in the present file to be consistently higher than the girls ratio ( greater than 50% and on average they are close to each other when comparing the past to the present). Interesting is the ratio seems to be declining a bit as we move from 1940 to 2002. Maybe a trend to watch and explore why if it continues to move towards the 50% mark.
# Insert code for Exercise 6 here
present <- present %>%
mutate(total = boys + girls)
present<-present %>%
mutate(boys_ratio_present=boys/total)
ggplot(data=present, aes(x=year, y=boys_ratio_present))+geom_point()In the datafile Present, the most total births in the US occur in 1961 at 4,268,326 total births.
Note: Also was able to look at finding a max and an index value but the sort approach seemed easiest to get the year and max value together.
# Insert code for Exercise 7 here
present <- present %>%
mutate(total = boys + girls)
arbuthnot %>%
summarize(min = min(boys), max = max(total))## # A tibble: 1 × 2
## min max
## <int> <int>
## 1 2890 16145
# Finds what is the max value of the total in the present frame and at what index is the max value (not so useful if you sort on any key other than year). ( couple of different ways to do this in R as shown.)
max(present$total)## [1] 4268326
which.max(present$total)## [1] 22
library(dplyr)
present %>%
group_by(year) %>%
slice(which.max(total))## # A tibble: 63 × 5
## # Groups: year [63]
## year boys girls total boys_ratio_present
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1940 1211684 1148715 2360399 0.513
## 2 1941 1289734 1223693 2513427 0.513
## 3 1942 1444365 1364631 2808996 0.514
## 4 1943 1508959 1427901 2936860 0.514
## 5 1944 1435301 1359499 2794800 0.514
## 6 1945 1404587 1330869 2735456 0.513
## 7 1946 1691220 1597452 3288672 0.514
## 8 1947 1899876 1800064 3699940 0.513
## 9 1948 1813852 1721216 3535068 0.513
## 10 1949 1826352 1733177 3559529 0.513
## # … with 53 more rows
#Arrange data frame present in descending order on sort key total then look at first colunm item. The first column in is the year of the max value and has the max value total.
present<- arrange(present, -total)
glimpse(present)## Rows: 63
## Columns: 5
## $ year <dbl> 1961, 1960, 1957, 1959, 1958, 1962, 1956, 1990, 199…
## $ boys <dbl> 2186274, 2179708, 2179960, 2173638, 2152546, 213246…
## $ girls <dbl> 2082052, 2078142, 2074824, 2071158, 2051266, 203489…
## $ total <dbl> 4268326, 4257850, 4254784, 4244796, 4203812, 416736…
## $ boys_ratio_present <dbl> 0.5122088, 0.5119269, 0.5123550, 0.5120713, 0.51204…
#Arrange data again back in ascending order by year
present<-arrange(present,year)
glimpse(present)## Rows: 63
## Columns: 5
## $ year <dbl> 1940, 1941, 1942, 1943, 1944, 1945, 1946, 1947, 194…
## $ boys <dbl> 1211684, 1289734, 1444365, 1508959, 1435301, 140458…
## $ girls <dbl> 1148715, 1223693, 1364631, 1427901, 1359499, 133086…
## $ total <dbl> 2360399, 2513427, 2808996, 2936860, 2794800, 273545…
## $ boys_ratio_present <dbl> 0.5133386, 0.5131376, 0.5141926, 0.5138001, 0.51356…