Introduction to R and RStudio Lab

source("E:/github/MS/DATA606/Lab1/more/arbuthnot.R")
source("E:/github/MS/DATA606/Lab1//more/present.R")

Exercise 1

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

# Get the variables used in the dataset
names(arbuthnot)
## [1] "year"  "boys"  "girls"
# Count of no.of girls in arbuthnot dataset
arbuthnot$girls
##  [1] 4683 4457 4102 4590 4839 4820 4928 4605 4457 4952 4784 5332 5200 4910
## [15] 4617 3997 3919 3395 3536 3181 2746 2722 2840 2908 2959 3179 3349 3382
## [29] 3289 3013 2781 3247 4107 4803 4881 5681 4858 4319 5322 5560 5829 5719
## [43] 6061 6120 5822 5738 5717 5847 6203 6033 6041 6299 6533 6744 7158 7127
## [57] 7246 7119 7214 7101 7167 7302 7392 7316 7483 6647 6713 7229 7767 7626
## [71] 7452 7061 7514 7656 7683 5738 7779 7417 7687 7623 7380 7288

Exercise 2

Is there an apparent trend in the number of girls baptized over the years? How would you describe it?

# Plot describing number of girls with respect to years
plot(arbuthnot$year, arbuthnot$girls, type = "l")

Exercise 3

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

# Plot a graph for boy proportion over years
boy_proportion = arbuthnot$boys / (arbuthnot$boys + arbuthnot$girls)
plot(arbuthnot$year, boy_proportion, type = "l", col = "dark Orange")

On Your Own

1.What years are included in this data set? What are the dimensions of the data frame and what are the variable or column names?

# years included in the dataset
present$year
##  [1] 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953
## [15] 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
## [29] 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981
## [43] 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995
## [57] 1996 1997 1998 1999 2000 2001 2002
# Dimensions used in the dataset
dim(present)
## [1] 63  3
# Variables and colum names used in the dataset
names(present)
## [1] "year"  "boys"  "girls"

2.How do these counts compare to Arbuthnot’s? Are they on a similar scale?

#comparing years 
range(arbuthnot$year)
## [1] 1629 1710
range(present$year)
## [1] 1940 2002
##comparing dimentions
dim(arbuthnot)
## [1] 82  3
dim(present)
## [1] 63  3
##comparing variables
names(arbuthnot)
## [1] "year"  "boys"  "girls"
names(present)
## [1] "year"  "boys"  "girls"

By looking at the above results we can observe that 1) The variables used in both datasets are same. 2) The dimensions are not on a same scale. 3) The date range for Arbuthnot and Present are not on same scale.

3.Make a plot that displays the boy-to-girl ratio for every year in the data set. What do you see? Does Arbuthnot’s observation about boys being born in greater proportion than girls hold up in the U.S.? Plot included

# Get the ratio in both datasets
boy_girl_ratio_present <- present$boys/present$girls
boy_girl_ratio_arbuthnot <- arbuthnot$boys/arbuthnot$girls

# Plot 2 graphs for comparision
par(mfrow=c(1,2))
plot( present$year, boy_girl_ratio_present, type="l", col="blue" )
plot( arbuthnot$year, boy_girl_ratio_arbuthnot, type="l", col="dark grey" )

Based on this we can observer that from boys to girls ratio keeps decreasing in Present dataset when compared to Arbuthnot’s.

4.In what year did we see the most total number of births in the U.S.?

final_ds <- data.frame(present$year, present$boys + present$girls)
names(final_ds)
## [1] "present.year"                 "present.boys...present.girls"
names(final_ds) <- c("Year", "Births")
names(final_ds)
## [1] "Year"   "Births"
# Order by Births descending
final_ds <- final_ds[order(final_ds$Births,decreasing = TRUE),]
# Show the year with the most births
head(final_ds, n=1)
##    Year  Births
## 22 1961 4268326