install.packages("tidyverse")
install.packages("openintro")library(tidyverse)
library(openintro)A peek at the data.
data('arbuthnot', package='openintro')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.
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…
This command should output the following
Rows: 82 Columns: 3 $ year
We can see that there are 82 observations and 3 variables in this
dataset. The variable names are year, boys,
and girls.
Let’s start to examine the data a little more closely. We can access the data in a single column of a data frame separately using a command like
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
This command will only show the number of boys baptized each year. The dollar sign basically says “go to the data frame that comes before me, and find the variable that comes after me”.
arbuthnot$girls
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
R has some powerful functions for making graphics. We can create a simple plot of the number of girls baptized per year with the command
ggplot(data = arbuthnot, aes(x = year, y = girls)) +
geom_point()We use the ggplot() function to build plots. Notice that
the command above again looks like a function, this time with arguments
separated by commas.
With ggplot():
aesthetic elements of the plot, e.g. the x and the y
axes.+ to
specify the geometric object for the plot. Since we want to
scatterplot, we use geom_point().For instance, if you wanted to visualize the above plot using a line
graph, you would replace geom_point() with
geom_line().
ggplot(data = arbuthnot, aes(x = year, y = girls)) +
geom_line()Try the following in your console:
?ggplotYes, there is an apparent upward trend in the number of girls baptized over the years
5218 + 4683## [1] 9901
to see the total number of baptisms in 1629. We could repeat this once for each year, but there is a faster way. If we add the vector for baptisms for boys to that of girls, R will compute all 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
What you will see are 82 numbers (in that packed display, because we aren’t looking at a data frame here), each one representing the sum we’re after. Take a look at a few of them and verify that they are right.
We’ll be using this new vector to generate some plots, so we’ll want to save it as a permanent column in our data frame.
The %>% operator is called the
piping operator. It takes the output of the previous
expression and pipes it into the first argument of the function in the
following one. To continue our analogy with mathematical functions,
x %>% f(y) is equivalent to f(x, y).
A note on piping: Note that we can read these two lines of code as the following:
“Take the arbuthnot dataset and
pipe it into the mutate function. Mutate
the arbuthnot data set by creating a new variable called
total that is the sum of the variables called
boys and girls. Then assign the resulting
dataset to the object called arbuthnot, i.e. overwrite the
old arbuthnot dataset with the new one containing the new
variable.”
You can make a line plot of the total number of baptisms per year with the command
ggplot(data = arbuthnot, aes(x = year, y = total)) +
geom_line()Similarly to you we computed the total number of births, you can compute the ratio of the number of boys to the number of girls baptized in 1629 with
5218 / 4683## [1] 1.114243
or you can act on the complete columns with the expression
You can also compute the proportion of newborns that are boys in 1629
5218 / (5218 + 4683)## [1] 0.5270175
or you can compute this for all years simultaneously and append it to the dataset
Note that we are using the new total variable we created
earlier in our calculations.
ggplot(data = arbuthnot, aes(x = year, y = boys))+ geom_line()
** One notices the similarity (an upward trend) between the graph representing the proportion of boys over time and the graph representing the numbers of girls over time. numbers and proportions seem to yield the same graphic. **
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 ask if the number of births of boys
outnumber that of girls in each year with the expression
arbuthnot <- arbuthnot %>%
mutate(more_boys = boys > girls)
print(arbuthnot)## # A tibble: 82 × 7
## year boys girls total boy_to_girl_ratio boy_ratio more_boys
## <int> <int> <int> <int> <dbl> <dbl> <lgl>
## 1 1629 5218 4683 9901 1.11 0.527 TRUE
## 2 1630 4858 4457 9315 1.09 0.522 TRUE
## 3 1631 4422 4102 8524 1.08 0.519 TRUE
## 4 1632 4994 4590 9584 1.09 0.521 TRUE
## 5 1633 5158 4839 9997 1.07 0.516 TRUE
## 6 1634 5035 4820 9855 1.04 0.511 TRUE
## 7 1635 5106 4928 10034 1.04 0.509 TRUE
## 8 1636 4917 4605 9522 1.07 0.516 TRUE
## 9 1637 4703 4457 9160 1.06 0.513 TRUE
## 10 1638 5359 4952 10311 1.08 0.520 TRUE
## # ℹ 72 more rows
This command adds a new variable to the arbuthnot data
frame containing the values of either TRUE if that year had
more boys than girls, or FALSE if that year did not (the
answer may surprise you). This variable contains a different kind of
data than we have encountered so far. All other columns in the
arbuthnot data frame have values that are numerical (the
year, the number of boys and girls). Here, we’ve asked R to create
logical data, data where the values are either
TRUE or FALSE. In general, data analysis will
involve many different kinds of data types, and one reason for using R
is that it is able to represent and compute with many of them.
Your assignment involves repeating the above steps, but for present
day birth records in the United States. The data are stored in a data
frame called present.
This analysis will use the present data set, a data frame that compiles birth rates in the U.S.A.from 1940 to 2002. The package was written by (Çetinkaya-Rundel et al. 2022) and is accessible from openintro-package and retrievable through: https://www.openintro.org/book/statdata/index.php?data=present
data('present', package='openintro')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 ...
names(present)## [1] "year" "boys" "girls"
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:
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
present %>%
summarize(min = min(boys), max = max(boys))## # A tibble: 1 × 2
## min max
## <dbl> <dbl>
## 1 1211684 2186274
present %>%
summarize(min = min(girls), max = max(girls))## # A tibble: 1 × 2
## min max
## <dbl> <dbl>
## 1 1148715 2082052
present$boys + present$girls## [1] 2360399 2513427 2808996 2936860 2794800 2735456 3288672 3699940 3535068
## [10] 3559529 3554149 3750850 3846986 3902120 4017362 4047295 4163090 4254784
## [19] 4203812 4244796 4257850 4268326 4167362 4098020 4027490 3760358 3606274
## [28] 3520959 3501564 3600206 3731386 3555970 3258411 3136965 3159958 3144198
## [37] 3167788 3326632 3333279 3494398 3612258 3629238 3680537 3638933 3669141
## [46] 3760561 3756547 3809394 3909510 4040958 4158212 4110907 4065014 4000240
## [55] 3952767 3899589 3891494 3880894 3941553 3959417 4058814 4025933 4021726
** The data set is a 63 * 3 table that covers the years 1940 - 2002 and contains 3 unique variables and 63 unique observations. The names of the variables are: year, boys, girls. All the three variables are numeric variables.**
** No, they are not of a similar magnitude. The counts in the present data frame are of a much bigger magnitude in their scope when compared to the counts in the Arbuthnot data set**
# Data visualization/Graphics
Rows: 63 Columns: 4 $ year
present <-present %>%
mutate ( boy_ratio = boys/present$boys +present$girls)
print(present)## # A tibble: 63 × 5
## year boys girls total boy_ratio
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1940 1211684 1148715 2360399 1148716
## 2 1941 1289734 1223693 2513427 1223694
## 3 1942 1444365 1364631 2808996 1364632
## 4 1943 1508959 1427901 2936860 1427902
## 5 1944 1435301 1359499 2794800 1359500
## 6 1945 1404587 1330869 2735456 1330870
## 7 1946 1691220 1597452 3288672 1597453
## 8 1947 1899876 1800064 3699940 1800065
## 9 1948 1813852 1721216 3535068 1721217
## 10 1949 1826352 1733177 3559529 1733178
## # ℹ 53 more rows
Alternative approach:
present <-present %>%
mutate ( boy_ratio = boys/total)
print(present)## # A tibble: 63 × 5
## year boys girls total boy_ratio
## <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
## # ℹ 53 more rows
present <- present %>%
mutate(boy_to_girl_ratio = boys / girls)
print(present)## # A tibble: 63 × 6
## year boys girls total boy_ratio boy_to_girl_ratio
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1940 1211684 1148715 2360399 0.513 1.05
## 2 1941 1289734 1223693 2513427 0.513 1.05
## 3 1942 1444365 1364631 2808996 0.514 1.06
## 4 1943 1508959 1427901 2936860 0.514 1.06
## 5 1944 1435301 1359499 2794800 0.514 1.06
## 6 1945 1404587 1330869 2735456 0.513 1.06
## 7 1946 1691220 1597452 3288672 0.514 1.06
## 8 1947 1899876 1800064 3699940 0.513 1.06
## 9 1948 1813852 1721216 3535068 0.513 1.05
## 10 1949 1826352 1733177 3559529 0.513 1.05
## # ℹ 53 more rows
present <- present %>%
mutate(more_boys = boys > girls)
view(present)Below is a line plot displaying the proportion of boys born over time:
ggplot(data = present, aes(x = year, y = boy_ratio)) +
geom_line() # number of boys born over time
ggplot(data = present, aes(x=year, y=boys)) +
geom_line()# number of girls born over time
ggplot(data = present, aes(x=year, y=girls)) +
geom_line()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.
ggplot(data = present, aes(x = year , y = boy_to_girl_ratio ))+
geom_line()present <- present %>%
mutate(more_boys = boys > girls)
view(present)arbuthnot <- arbuthnot %>%
mutate(more_boys = boys > girls)
view (arbuthnot)** Yes, Arbuthnot’s observation about boys being born in greater proportion than girls hold up in the U.S. However, the overall trend in birth rates is downward in the U.S. for both gender, boys and girls is downward **
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 %>%
arrange(desc(total))Year with the highest total number of births: was 1961
These data come from reports by the Centers for Disease Control. You
can learn more about them by bringing up the help file using the command
?present.
That was a short introduction to R and RStudio, but we will provide you with more functions and a more complete sense of the language as the course progresses.
In this course we will be using the suite of R packages from the tidyverse. The book R For Data Science by Grolemund and Wickham is a fantastic resource for data analysis in R with the tidyverse. If you are googling for R code, make sureto also include these package names in your search query. For example, instead of googling “scatterplot in R”, google “scatterplot in R with the tidyverse”.
These cheatsheets may come in handy throughout the semester: