R is an open-source programming language, meaning that users can contribute packages that make our lives easier, and we can use them for free. For this lab, and many others in the future, we will use the following R packages:
To get started, let’s take a peek at the data.
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…
What command would you use to extract just the counts of girls baptized? Try it!
## [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. If you run the plotting
code in your console, you should see the plot appear under the Plots tab
of the lower right panel of RStudio. Notice that the command above again
looks like a function, this time with arguments separated by commas.
With ggplot():
ggplot(data = arbuthnot, aes(x = year, y = girls)) +
geom_line()
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.)
There is a noticeable trend in the number of girls being baptized as we can see in the 1640’s there is a relative decline in baptisms as we approach the 1660’s there is a noticeable upward trend after this period.There is a slight dip in the early 1700’s but there is a continued trend upwards after this period.
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
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.
arbuthnot <- arbuthnot %>%
mutate(total = boys + girls)
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
arbuthnot <- arbuthnot %>%
mutate(boy_to_girl_ratio = boys / girls)
or you can compute this for all years simultaneously and append it 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(data = arbuthnot, aes( x = year, y = boy_ratio)) + geom_line()
The ratio of boys seems to be very volatile but there seems to be a downward trend for the ratio of boys after 1660.
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)
In the previous few pages, you recreated some of the displays and preliminary analysis of Arbuthnot’s baptism data. Your assignment involves repeating these steps, but for present day birth records in the United States. The data are stored in a data frame called present.
data('present', package='openintro')
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
arbuthnot %>%
summarize(min = min(boys), max = max(boys))
What years are included in this data set? What are the dimensions of the data frame? What are the variable (column) names?
Present <- present
print (range(present$year))
## [1] 1940 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…
The years are from 1940 to 2002 There are 3 columns and 63 rows The variable column names are years,boys and girls.
How do these counts compare to Arbuthnot’s? Are they of a similar magnitude?
There are the same column length but the amount of rows are different in magnitude as Arbuthnot’s has 82 observations while this current dataset has 63.
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 <- present %>%
mutate (total = boys + girls,
boys_ratio = boys/total,
more_boys = boys > girls)
ggplot (present, aes(x=year,y=boys_ratio)) + geom_line()
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 data set 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.
The year with with the most total number of births in the US was 1961 with 4,268,326 births
present %>%
arrange(desc(total))