1. Synopsis

The sinking of the RMS Titanic occurred on the night of 14 April through to the morning of 15 April 1912 in the North Atlantic Ocean, four days into the ship’s maiden voyage from Southampton to New York City. The largest passenger liner in service at the time, Titanic had an estimated 2,224 people on board when she struck an iceberg at around 23:40 (ship’s time) on Sunday, 14 April 1912. Her sinking two hours and forty minutes later at 02:20 (05:18 GMT) on Monday, 15 April resulted in the deaths of more than 1,500 people, which made it one of the deadliest peacetime maritime disasters in history.

2. Exercise

Read the data and store it in a dataframe “titanic.df”

titanic.df<-read.csv(paste("Titanic Data.csv",sep=""))
View(titanic.df)

Use R to count the total number of passengers on board the Titanic.

Determine the total number of passengers aboard the Titanic

titanic.df<-read.csv(paste("Titanic Data.csv",sep=""))
nrow(titanic.df) #Total No. of passengers aboard 
## [1] 889

The total number of passengers aboard the Titanic is 889.

Use R to count the number of passengers who survived the sinking of the Titanic.

table(titanic.df$Survived) #Total No. of passengers survived 
## 
##   0   1 
## 549 340

The total number of passengers surviving the sinking of the Titanic is 340.

Use R to measure the percentage of passengers who survived the sinking of the Titanic.

mytable<- with(titanic.df,table(Survived)) #Table of passengers surviving
prop.table(mytable)*100 #Percent of passengers surviving
## Survived
##        0        1 
## 61.75478 38.24522

The % of passengers surviving the sinking of the Titanic is 38.24522.

Use R to count the number of first-class passengers who survived the sinking of the Titanic.Also use R to measure the percentage of first-class passengers who survived the sinking of the Titanic.

mytable <- xtabs(~Survived + Pclass, data=titanic.df) #No. of first class passengers surviving
mytable
##         Pclass
## Survived   1   2   3
##        0  80  97 372
##        1 134  87 119
prop.table(mytable,2)*100 #% of first class passengers surviving
##         Pclass
## Survived        1        2        3
##        0 37.38318 52.71739 75.76375
##        1 62.61682 47.28261 24.23625

The number of first class passengers who survived is 134. The % of first class passengers who survived is 62.61682.

Use R to count the number of females from First-Class who survived the sinking of the Titanic.Also use R to measure the percentage of survivors who were female

mytable <- xtabs(~Survived + Pclass + Sex, data=titanic.df) #No. of first class female passengers surviving
mytable
## , , Sex = female
## 
##         Pclass
## Survived   1   2   3
##        0   3   6  72
##        1  89  70  72
## 
## , , Sex = male
## 
##         Pclass
## Survived   1   2   3
##        0  77  91 300
##        1  45  17  47
mytable<-xtabs(~Survived+Sex, data=titanic.df)#% of females passengers surviving
prop.table(mytable,2)*100
##         Sex
## Survived   female     male
##        0 25.96154 81.10919
##        1 74.03846 18.89081

The number of first class female passengers surviving is 89. The % of female passengers on board who survived is 74.03846.

Run a Pearson’s Chi-squared test to test the following hypothesis:

Hypothesis: The proportion of females onboard who survived the sinking of the Titanic was higher than the proportion of males onboard who survived the sinking of the Titanic.

mytable <- xtabs(~Survived + Sex, data=titanic.df) #No. of female passengers surviving
chisq.test(mytable)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  mytable
## X-squared = 258.43, df = 1, p-value < 2.2e-16

The p value for the Pearson’s Chi-squared test is 2.2 e-16, which is <0.05. So, we can reject our null hypothesis that the sex of a person and his/her survival are independent of each other.

Conclusion

The proportion of females on board who survived the sinking of the Titanic was higher than the proportion of males on board who survived the sinking of the Titanic.