R Markdown

Reading the dataset.

setwd("~/R")
titanic.df<-read.csv(paste("Titanic Data.csv",sep = ""))

Count the total number of passengers on board the Titanic.

 nrow(titanic.df)
## [1] 889

Count the number of passengers who survived the sinking of the Titanic.

mytable <- with(titanic.df, table(Survived))
mytable
## Survived
##   0   1 
## 549 340

Measure the percentage of passengers who survived the sinking of the Titanic.

mytable <- with(titanic.df, table(Survived))
prop.table(mytable)*100
## Survived
##        0        1 
## 61.75478 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.

mytable <- xtabs(~ Pclass+Survived, data=titanic.df)
prop.table(mytable)*100
##       Survived
## Pclass         0         1
##      1  8.998875 15.073116
##      2 10.911136  9.786277
##      3 41.844769 13.385827

Count the number of females from First-Class who survived the sinking of the Titanic.

mytable <- xtabs(~ Pclass+Survived+Sex, data=titanic.df)
mytable
## , , Sex = female
## 
##       Survived
## Pclass   0   1
##      1   3  89
##      2   6  70
##      3  72  72
## 
## , , Sex = male
## 
##       Survived
## Pclass   0   1
##      1  77  45
##      2  91  17
##      3 300  47

Count the percentage of females from First-Class who survived the sinking of the Titanic.

mytable <- xtabs(~ Pclass+Survived+Sex, data=titanic.df)
prop.table(mytable)*100
## , , Sex = female
## 
##       Survived
## Pclass          0          1
##      1  0.3374578 10.0112486
##      2  0.6749156  7.8740157
##      3  8.0989876  8.0989876
## 
## , , Sex = male
## 
##       Survived
## Pclass          0          1
##      1  8.6614173  5.0618673
##      2 10.2362205  1.9122610
##      3 33.7457818  5.2868391

Measure the percentage of females on board the Titanic who survived.

mytable <- xtabs(~ Survived+Sex, data=titanic.df)
prop.table(mytable)*100
##         Sex
## Survived    female      male
##        0  9.111361 52.643420
##        1 25.984252 12.260967

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)
addmargins(mytable)
##         Sex
## Survived female male Sum
##      0       81  468 549
##      1      231  109 340
##      Sum    312  577 889
 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

This output suggests a relationship between ‘Sex’ and ‘Survived’.