DATA606 Lab01-Intro to R

Author

Ziaul Hoque

This is Lab01 exercise for DATA606

Load all package related to Lab assignment

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.1     
── 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)
Loading required package: airports
Loading required package: cherryblossom
Loading required package: usdata
library(ggplot2)

Load Dr. Arbuthnot’s Baptism Records.

data('arbuthnot', package = 'openintro')
dim(arbuthnot)
[1] 82  3
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…

Exercise 1

What command would you use to extract just the counts of girls baptized?

Ans: 82

girls_record<-arbuthnot$girls
length(girls_record)
[1] 82

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.)

Ans: The number of girls baptized over the years is increasing therefore there is a positive linear.

ggplot(data = arbuthnot, aes(x = year, y = girls)) + 
  geom_point() +
  geom_line() +
  labs(
    title = "Number of Girls Baptized Over Time",
    x = "Year",
    y = "Girls Baptized"
  )

Exercise 3

Now, generate a plot of the proportion of boys born over time. What do you see?

Ans: The number of boys baptized increased since 1660 and it became positive linear.

arbuthnot <- arbuthnot %>%
  mutate(total = boys + girls)
arbuthnot <- arbuthnot %>%
  mutate(boy_to_girl_ratio = boys / girls)
arbuthnot <- arbuthnot %>%
  mutate(boy_ratio = boys / total)

ggplot(data = arbuthnot, aes(x = year, y = boy_ratio)) + 
  geom_line() +
  labs(
    title = "Number of Boys baptized ratio over Time",
    x = "Year",
    y = "Boys_ratio baptized"
  )

More Practice

data('present', package='openintro')
arbuthnot %>%
  summarize(min = min(boys), max = max(boys))
# A tibble: 1 × 2
    min   max
  <int> <int>
1  2890  8426

Exercise 4

What years are included in this data set? What are the dimensions of the data frame? What are the variable (column) names?

Answer -

  • Year range: 1940 to 2002

  • dimension of the data frame: 63 x 3

  • Variable: “year” “boys” “girls”

range(present$year)
[1] 1940 2002
dim(present)
[1] 63  3
names(present)
[1] "year"  "boys"  "girls"

Exercise 5

How do these counts compare to Arbuthnot’s? Are they of a similar magnitude?

Ans: No, they are different than Arbuthnot’s, they are in millions and ranges from 1940 - 2002 and the Arbuthnot’s in thousands and ranges from 1629 - 1710.

arbuthnot$girls + arbuthnot$boys
 [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
present$girls + present$boys
 [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
range(present$year)
[1] 1940 2002
range(arbuthnot$year)
[1] 1629 1710

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.

Ans: The proportion of boys had increased in the sixties and then declined in late seventies then increased again in the eighties. And yes, Arbuthnot’s observation about boys being born in greater proportion than girls hold up in the U.S. is ture.

present <- present %>%
  mutate(total = boys + girls)
present <- present %>%
  mutate(boy_to_girl_ratio = boys / girls)
present <- present %>%
  mutate(boy_ratio = boys / total)

ggplot(data = present, aes(x = year, y = boy_ratio)) + 
  geom_line() +
  labs(
    title = "Boys ratio over Time from Present Dataset",
    x = "Year",
    y = "Boys_ratio"
  )

arbuthnot <- arbuthnot %>%
  mutate(total = boys + girls)
arbuthnot <- arbuthnot %>%
  mutate(boy_to_girl_ratio = boys / girls)
arbuthnot <- arbuthnot %>%
  mutate(boy_ratio = boys / total)

ggplot(data = arbuthnot, aes(x = year, y = boy_ratio)) + 
  geom_line() +
  labs(
    title = "Boys ratio over Time from Arbuthnot Dataset",
    x = "Year",
    y = "Boys_ratio"
  )

The graph is very different from Arbuthnot’s observation, the boy_ratio decreasing with 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.

Ans: The most birth year is 1961

present <- present %>%
  mutate(total = boys + girls)
present <- present %>%
  arrange(desc(total))
head(present, 3)
# A tibble: 3 × 6
   year    boys   girls   total boy_to_girl_ratio boy_ratio
  <dbl>   <dbl>   <dbl>   <dbl>             <dbl>     <dbl>
1  1961 2186274 2082052 4268326              1.05     0.512
2  1960 2179708 2078142 4257850              1.05     0.512
3  1957 2179960 2074824 4254784              1.05     0.512

or we can get maximum total population’s of year through ‘which.max’ function -

present[which.max(present$total), 1]
# A tibble: 1 × 1
   year
  <dbl>
1  1961