titanic<-read.csv(paste("Titanic Data.csv",sep=" "))
View(titanic)
Q1-Use R to count the total number of passengers on board the Titanic.
dim(titanic)
## [1] 889 8
Ans-A total of 889 passengers on board.
Q2-Use R to count the number of passengers who survived the sinking of the Titanic.
library(psych)
table(titanic$Survived=="1")
##
## FALSE TRUE
## 549 340
Ans-340 people survived the sinking of Titanic.
Q3-Use R to measure the percentage of passengers who survived the sinking of the Titanic.
prop.table(table(titanic$Survived=="1"))*100
##
## FALSE TRUE
## 61.75478 38.24522
Ans-38.24% people survived the sinking of the ship.
Q4-Use R to count the number of first-class passengers who survived the sinking of the Titanic.
mytable<-xtabs(~Survived+Pclass,data=titanic)
mytable
## Pclass
## Survived 1 2 3
## 0 80 97 372
## 1 134 87 119
Ans-134 people survived the Titanic who were first-class passengers.
Q5-Use R to measure the percentage of first-class passengers who survived the sinking of the Titanic
prop.table(mytable)*100
## Pclass
## Survived 1 2 3
## 0 8.998875 10.911136 41.844769
## 1 15.073116 9.786277 13.385827
Ans-15.07% of people were first-class passengers who survived the sinking of the Titanic.
Q6-Use R to count the number of females from First-Class who survived the sinking of the Titanic.
mytable<-xtabs(~Sex+Pclass+Survived,data=titanic)
ftable(mytable)
## Survived 0 1
## Sex Pclass
## female 1 3 89
## 2 6 70
## 3 72 72
## male 1 77 45
## 2 91 17
## 3 300 47
Ans-89 females from First-Class survived the sinking of the Titanic.
Q7-Use R to measure the percentage of survivors who were female.
mytable<-xtabs(~Sex+Survived,data = titanic)
prop.table(mytable,2)*100
## Survived
## Sex 0 1
## female 14.75410 67.94118
## male 85.24590 32.05882
Ans-67.94% were survivors who were female.
Q8-Use R to measure the percentage of females on board the Titanic who survived.
prop.table(mytable,1)*100
## Survived
## Sex 0 1
## female 25.96154 74.03846
## male 81.10919 18.89081
Ans-74.03% were females on board the Titanic who survived.
Q9-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(mytable)
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: mytable
## X-squared = 258.43, df = 1, p-value < 2.2e-16
Ans-As p-value is very small,smaller than 5% therefore our hypothesis is true.The data is dependant.