Task 2b
Read the Titanic data set into R. Create a dataframe called "titanic":
setwd("C:/Users/ASHUTOSH/Documents/R/iim")
titanic<-read.csv("Titanic Data.csv")
View(titanic)
Task 3a
Use R to count the total number of passengers on board the Titanic:
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 ..
Task 3b
Use R to count the number of passengers who survived the sinking of the Titanic.
titanic.survived <- table(titanic$Survived)
titanic.survived
0 1
549 340
Task 3c
Use R to measure the percentage of passengers who survived the sinking of the Titanic
prop.table(titanic.survived) *100
0 1
61.75478 38.24522
Task 3d
Use R to count the number of first-class passengers who survived the sinking of the Titani
titanic.survived2 <- xtabs(~Survived+Pclass, data=titanic)
addmargins(titanic.survived2)
Pclass
Survived 1 2 3 Sum
0 80 97 372 549
1 134 87 119 340
Sum 214 184 491 889
Task 3e
Use R to measure the percentage of first-class passengers who survived the sinking of the Titanic.
prop.table(titanic.survived2, 2) *100
Pclass
Survived 1 2 3
0 37.38318 52.71739 75.76375
1 62.61682 47.28261 24.23625
Task 3f
Use R to count the number of females from First-Class who survived the sinking of the Titanic
titanic.survived3 <- xtabs(~Survived+Pclass+Sex, data=titanic)
titanic.survived3
, , 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
Task 3g
Use R to measure the percentage of survivors who were female
titanic.survived4 <- xtabs(~Survived+Sex, data = titanic)
titanic.survived4
Sex
Survived female male
0 81 468
1 231 109
Task 3h
Use R to measure the percentage of females on board the Titanic who survived
prop.table(titanic.survived4,1) *10
Sex
Survived female male
0 1.475410 8.524590
1 6.794118 3.205882
Task 3i
Run 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(titanic.survived4)
Pearson's Chi-squared test with Yates' continuity correction
data: titanic.survived4
X-squared = 258.43, df = 1, p-value < 2.2e-16