Reading the Titanic data set into R. Create a dataframe called “Titanic”.

setwd("C:/Users/vaibhav/Desktop")
Titanic.df<-read.csv("TitanicData.csv")
View(Titanic.df)

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

dim(Titanic.df)
## [1] 889   8

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

my.table<-with(Titanic.df,table(Survived))
my.table
## Survived
##   0   1 
## 549 340

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

prop.table(my.table)*100
## Survived
##        0        1 
## 61.75478 38.24522

Using R to count the number of first-class passengers who survived the sinking of the Titanic.

my.table<-xtabs(~ Survived+Pclass,data=Titanic.df)
my.table
##         Pclass
## Survived   1   2   3
##        0  80  97 372
##        1 134  87 119

Using R to measure the percentage of first-class passengers who survived the sinking of the Titanic.

prop.table(my.table,2)*100
##         Pclass
## Survived        1        2        3
##        0 37.38318 52.71739 75.76375
##        1 62.61682 47.28261 24.23625

Using R to count the number of females from First-Class who survived the sinking of the Titanic

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

Using R to measure the percentage of survivors who were female

my.table<-xtabs(~Survived+Sex,data=Titanic.df)
my.table
##         Sex
## Survived female male
##        0     81  468
##        1    231  109
prop.table(my.table,1)*100
##         Sex
## Survived   female     male
##        0 14.75410 85.24590
##        1 67.94118 32.05882

Using R to measure the percentage of females on board the Titanic who survived

my.table<-xtabs(~Survived+Sex,data=Titanic.df)
my.table
##         Sex
## Survived female male
##        0     81  468
##        1    231  109
prop.table(my.table,2)*100
##         Sex
## Survived   female     male
##        0 25.96154 81.10919
##        1 74.03846 18.89081

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

chisq.test(my.table)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  my.table
## X-squared = 258.43, df = 1, p-value < 2.2e-16