#Loading and viewing data
titanic <- read.csv("Titanic Data.csv")
View(titanic)
#Total Number of passengers.
str(titanic)
## 'data.frame':    889 obs. of  8 variables:
##  $ Survived: int  0 1 1 1 0 0 0 0 1 1 ...
##  $ Pclass  : int  3 1 3 1 3 3 1 3 3 2 ...
##  $ Sex     : Factor w/ 2 levels "female","male": 2 1 1 1 2 2 2 2 1 1 ...
##  $ Age     : num  22 38 26 35 35 29.7 54 2 27 14 ...
##  $ SibSp   : int  1 1 0 1 0 0 0 3 0 1 ...
##  $ Parch   : int  0 0 0 0 0 0 0 1 2 0 ...
##  $ Fare    : num  7.25 71.28 7.92 53.1 8.05 ...
##  $ Embarked: Factor w/ 3 levels "C","Q","S": 3 1 3 3 3 2 3 3 3 1 ...
##Hence total number of passengers are 889.
#Number of passengers who survived(that is Survived=1)
titanicsurv <- table(titanic$Survived)
titanicsurv
## 
##   0   1 
## 549 340
##Hence only 340 people survived
#Percent of passengers who survived
prop.table(titanicsurv) *100
## 
##        0        1 
## 61.75478 38.24522
##Hence percentage of people survived is 38.24522
#The number of first-class passengers who survived the sinking of the Titanic
titanicsurv2 <- xtabs(~Survived+Pclass, data=titanic)
addmargins(titanicsurv2)
##         Pclass
## Survived   1   2   3 Sum
##      0    80  97 372 549
##      1   134  87 119 340
##      Sum 214 184 491 889
##This shows us that 134 of the 214 First Class passengers listed, survived.
#The percentage of first-class passengers who survived the sinking of the Titanic.
prop.table(titanicsurv2, 2) *100
##         Pclass
## Survived        1        2        3
##        0 37.38318 52.71739 75.76375
##        1 62.61682 47.28261 24.23625
##Hence the percentage is 62.61682
# The number of females from First-Class who survived the sinking of the Titanic
titanicsurv3 <- xtabs(~Survived+Pclass+Sex, data=titanic)
titanicsurv3
## , , 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
##Hence the number of females for th First class who survived is 89
#The percentage of survivors who were female
titanicsurv4 <- xtabs(~Survived+Sex, data = titanic)
prop.table(titanicsurv4,1) *100
##         Sex
## Survived   female     male
##        0 14.75410 85.24590
##        1 67.94118 32.05882
##The percentage of female survivors is 67.94118.
#The percentage of females on board the Titanic who survived
prop.table(titanicsurv4,2) *100
##         Sex
## Survived   female     male
##        0 25.96154 81.10919
##        1 74.03846 18.89081
##This shows that 74.03% of females on board the Titanic survived.
#Pearson’s Chi-squared test
chisq.test(titanicsurv4)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  titanicsurv4
## X-squared = 258.43, df = 1, p-value < 2.2e-16
##The p-value being less than 1 in 22 Quadrillion, it is safe to reject the null hypothesis that the survival rates for men and women were equal.