Task 2b- Reading the dataset

titanic<- read.csv("Titanic Data.csv")
View(titanic)

Task 3a

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

length(titanic$Sex)
## [1] 889

Task 3b

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

table(titanic$Survived[titanic$Survived==1])
## 
##   1 
## 340

Task 3c

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

passenger<- table(titanic$Survived)  #passenger holds survived vs nonsurvived
prop.table(passenger)*100
## 
##        0        1 
## 61.75478 38.24522
38.24522 
## [1] 38.24522

Task 3d

Use R to count the number of first-class passengers who survived the sinking of the Titanic. (Hint: You could use xtabs() )

Sur_class<- xtabs(~ Survived+Pclass, data=titanic) #two way table: Survived and Pclass
Sur_class[2,1]
## [1] 134

Task 3e

Use R to measure the percentage of first-class passengers who survived the sinking of the Titanic. (Hint: You could use prop.table() )

temp<- prop.table(Sur_class)*100
temp[2,1]
## [1] 15.07312

Task 3f

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

sex_class<- xtabs(~Sex+Pclass+Survived, data=titanic)  #two way table: Sex and Pclass
Female_1class_sur<- ftable(sex_class)
Female_1class_sur[1,2]
## [1] 89

Task 3g

Use R to measure the percentage of survivors who were female

sur_sex<- xtabs(~Survived+Sex, data=titanic)
addmargins(sur_sex,2)
##         Sex
## Survived female male Sum
##        0     81  468 549
##        1    231  109 340
prop.table(sur_sex,1)*100
##         Sex
## Survived   female     male
##        0 14.75410 85.24590
##        1 67.94118 32.05882
(231/340)*100
## [1] 67.94118

Task 3h

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

addmargins(sur_sex,1)
##         Sex
## Survived female male
##      0       81  468
##      1      231  109
##      Sum    312  577
prop.table(sur_sex,2)
##         Sex
## Survived    female      male
##        0 0.2596154 0.8110919
##        1 0.7403846 0.1889081
(231/312)*100
## [1] 74.03846

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.

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

We can not reject the Null hypothesis (p < 0.5). There is a relationship between peoples who survived and thier sex.