The total number of passengers on board the Titanic

total=length(titanic.df$Survived)
cat("Total passengers =", total)
## Total passengers = 889

The number of passengers who survived the sinking of the Titanic

survived=length(which(titanic.df$Survived==1))
cat("Number of survived passengers =", survived)
## Number of survived passengers = 340

The percentage of passengers who survived the sinking of the Titanic

survivedprop=100*survived/total
cat("Percentage of survived passengers =", survivedprop)
## Percentage of survived passengers = 38.24522

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

cs<-subset(titanic.df, titanic.df$Survived == 1 & titanic.df$Pclass == 1)
survivedfclass=length(which(cs$Survived==1))
cat("Number of survived first-class passengers =", survivedfclass)
## Number of survived first-class passengers = 134

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

fclass=length(which(titanic.df$Pclass==1))
cat("Number of first-class passengers =", fclass)
## Number of first-class passengers = 214
survivedfclassprop=100*survivedfclass/fclass
cat("Percentage of survived first-class passengers =", survivedfclassprop)
## Percentage of survived first-class passengers = 62.61682

The number of females from first-class who survived the sinking of the Titanic

fcs<-subset(titanic.df, titanic.df$Survived == 1 & titanic.df$Pclass == 1 & titanic.df$Sex == "female")
survivedfemalefclass=length(which(fcs$Survived==1))
cat("Number of survived female first-class passengers =", survivedfemalefclass)
## Number of survived female first-class passengers = 89

The percentage of survivors who were female

fs<-subset(titanic.df, titanic.df$Survived == 1 & titanic.df$Sex == "female")
survivedfemale=length(which(fs$Survived==1))
cat("Number of survived female passengers =", survivedfemale)
## Number of survived female passengers = 231
survivedfemaleprop=100*survivedfemale/survived
cat("Percentage of survived female passengers =", survivedfemaleprop)
## Percentage of survived female passengers = 67.94118

The percentage of females on board the Titanic who survived

female=length(which(titanic.df$Sex=="female"))
cat("Number of female passengers =", female)
## Number of female passengers = 312
femalesurvivedprop=100*survivedfemale/female
cat("Percentage of female survived passengers =", femalesurvivedprop)
## Percentage of female survived passengers = 74.03846

Pearson’s Chi-squared test to test the hypothesis between the proportion of males and females on board who survived the sinking of the Titanic

titanic.data=data.frame(titanic.df$Survived, titanic.df$Sex)
titanic.data=table(titanic.df$Survived, titanic.df$Sex)
cat("Table on survival of male and female passengers", "\n", titanic.data)
## Table on survival of male and female passengers 
##  81 231 468 109
print(chisq.test(titanic.data))
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  titanic.data
## X-squared = 258.43, df = 1, p-value < 2.2e-16