Load and review the Titanic Dataset
titanic.df <- read.csv(paste("Titanic Data.csv", sep=""))
View(titanic.df)
Count the total number of passengers on board the Titanic
count_OB <- nrow(titanic.df)
count_OB
## [1] 889
count the number of passengers who survived the sinking of the Titanic
count_sur <- sum(titanic.df$Survived == 1)
count_sur
## [1] 340
Measure the percentage of passengers who survived the sinking of the Titanic
per_sur <- (count_sur/count_OB) * 100
per_sur
## [1] 38.24522
Count the number of first-class passengers who survived the sinking of the Titanic
mytable <- xtabs(~ Pclass+Survived, data=titanic.df)
mytable
## Survived
## Pclass 0 1
## 1 80 134
## 2 97 87
## 3 372 119
Measure the percentage of first-class passengers who survived the sinking of the Titanic
prop.table(mytable, 1) * 100
## Survived
## Pclass 0 1
## 1 37.38318 62.61682
## 2 52.71739 47.28261
## 3 75.76375 24.23625
Count the number of females from First-Class who survived the sinking of the Titanic
mytable1 <- xtabs(~ Pclass+Sex+Survived, data=titanic.df)
mytable1
## , , Survived = 0
##
## Sex
## Pclass female male
## 1 3 77
## 2 6 91
## 3 72 300
##
## , , Survived = 1
##
## Sex
## Pclass female male
## 1 89 45
## 2 70 17
## 3 72 47
Measure the percentage of survivors who were female
mytable2 <- xtabs(~ Survived+Sex, data=titanic.df)
mytable2
## Sex
## Survived female male
## 0 81 468
## 1 231 109
prop.table(mytable2, 1) * 100
## Sex
## Survived female male
## 0 14.75410 85.24590
## 1 67.94118 32.05882
Measure the percentage of females on board the Titanic who survived
prop.table(mytable2, 2) * 100
## Sex
## Survived female male
## 0 25.96154 81.10919
## 1 74.03846 18.89081
Pearson’s Chi-squared test for hypothesis testing.
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.
addmargins(mytable2)
## Sex
## Survived female male Sum
## 0 81 468 549
## 1 231 109 340
## Sum 312 577 889
chisq.test(mytable2)
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: mytable2
## X-squared = 258.43, df = 1, p-value < 2.2e-16