Titanic

setwd("~/Desktop/5 SRM Kashish Mukheja/Downoad content")
Titanic.df<-read.csv(paste("Titanic Data.csv",sep=""))
View(Titanic.df)

3a.Code to count the total number of passengers on board the Titanic:

mytable<-with(Titanic.df,table(Survived))
summary(mytable)
## Number of cases in table: 889 
## Number of factors: 1

The total number of passengers on board the Titanic:889

3b.Code to count the number of passengers who survived the sinking of the Titanic:

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

The number of passengers who survived the sinking of the Titanic:340

3c.Code to measure the percentage of passengers who survived the sinking of the Titanic:

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

The percentage of passengers who survived the sinking of the Titanic:38.24522

3d.Code to count the number of first-class passengers who survived the sinking of the Titanic:

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

The number of first-class passengers who survived the sinking of the Titanic:134

3e.Code to measure the percentage of first-class passengers who survived the sinking of the Titanic:

prop.table(mytable1)*100
##         Pclass
## Survived         1         2         3
##        0  8.998875 10.911136 41.844769
##        1 15.073116  9.786277 13.385827

The percentage of first-class passengers who survived the sinking of the Titanic:15.073116

3f.Code to count the number of females from First-Class who survived the sinking of the Titanic:

mytable2<-xtabs(~Survived+Pclass+Sex,data=Titanic.df)
mytable2
## , , 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

The number of females from First-Class who survived the sinking of the Titanic:89

3g.Code to measure the percentage of survivors who were female:

ftable(prop.table(margin.table(mytable2,c(1,3))))*100
##          Sex    female      male
## Survived                        
## 0             9.111361 52.643420
## 1            25.984252 12.260967

The percentage of survivors who were female:25.984252

3h.Code to measure the percentage of females on board the Titanic who survived:

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

The percentage of females on board the Titanic who survived:74.03846

3i.Code to Run 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.

mytable4<-xtabs(~Survived+Sex,data=Titanic.df)
addmargins(mytable4)
##         Sex
## Survived female male Sum
##      0       81  468 549
##      1      231  109 340
##      Sum    312  577 889
chisq.test(mytable4)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  mytable4
## X-squared = 258.43, df = 1, p-value < 2.2e-16

Since the p-value<0.01, so we reject the null Hypothesis.