Type the name of each of these packages (tidyverse, openintro) into the search box to see if they have been installed
install.packages("tidyverse")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
install.packages("openintro")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
with the library function…load the tidyverse and openintro packages into your working environment.
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.0 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.1.8
## ✔ 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 ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
library(openintro)
## Loading required package: airports
## Loading required package: cherryblossom
## Loading required package: usdata
let’s take a peek at the data.
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
## # … with 72 more rows
You can see the dimensions of this data frame as well as the names of the variables and the first few observations by inserting the name of the dataset into the glimpse()
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…
We can access the data in a single column of a data frame by extracting the column with a $. For example, the code below extracts the boys column from the arbuthnot data frame.
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?
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
We can create a simple plot of the number of girls baptized per year with the following code:
ggplot(data=arbuthnot,aes(x=year, y=girls))+ geom_point()
we use geom_point()…This tells ggplot() to draw a line from each
observation with the next observation (sequentially).
ggplot(data=arbuthnot,aes(x=year,y=girls)) + geom_line()
###### Exercise 2 Is there an apparent trend in the number of girls
baptized over the years? How would you describe it? there appears
to be an overall positive trend in the number of girls baptized over the
years.
ggplot(data=arbuthnot,aes(x=year,y=girls))+ geom_point()+
geom_smooth(method = lm)
## `geom_smooth()` using formula = 'y ~ x'
To learn what a function does and how to use it (e.g. the function’s
arguments), just type in a question mark followed by the name of the
function that you’re interested in into the console.
?ggplot
suppose we want to plot the total number of baptisms… we can type in mathematical expressions such as the below calculation into the console.
5218+4683
## [1] 9901
If we add the vector for baptisms for boys to that of girls, R can compute each of these sums simultaneously.
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)
You can make a line plot of the total number of baptisms per year with the following code:
ggplot(data=arbuthnot,aes(x=year,y=total))+ geom_line()
In an similar fashion, once you know the total number of baptisms
for boys and girls in 1629, you can compute the ratio of the number of
boys to the number of girls baptized with the following code:
5218/4683
## [1] 1.114243
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
)
You can also compute the proportion of newborns that are boys in 1629 with the following code:
5218/(5218+4683)
## [1] 0.5270175
Or you can compute this for all years simultaneously and add it as a new variable named boy_ratio to the dataset:
arbuthnot<-arbuthnot%>%
mutate(boy_ratio=boys/total)
Now, generate a plot of the proportion of boys born over time. What do you see?
ggplot(arbuthnot,aes(x=year,y=boy_ratio))+geom_line()
Finally, 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
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%>%
summarize(min=min(boys), max=max(boys))
## # A tibble: 1 × 2
## min max
## <int> <int>
## 1 2890 8426
What years are included in this data set? What are the dimensions of the data frame? What are the variable (column) names? Included in this dataset are the years 1940-2002. Dimensions: 63 rows, 3 columns. Variable names include:year, boys, girls
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
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…
How do these counts compare to Arbuthnot’s? Are they of a similar magnitude? Although the arbuthnot data and the present data contain the same amount of columns, 3, the arbuthnot is larger in magnitude because it has more rows/ observations 82 and 63 comparatively
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…
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 name of the data frame. No, it does not hold up. With the present boys data, there is a pronounced downward trend.
present<-present%>%
mutate(present_total=boys+girls)
present<-present%>%
mutate(present_prop_boys=boys/present_total)
ggplot(present,aes(x=year,y=present_prop_boys))+geom_line()
ggplot(arbuthnot,aes(x=year,y=boy_ratio))+geom_line()
###### Exercise 7 In what year did we see the most total number of
births in the U.S.? 1961
present%>%
arrange(desc(present_total))
## # A tibble: 63 × 5
## year boys girls present_total present_prop_boys
## <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
## # … with 53 more rows