Titanic1

Om Joy Halder
26-09-2017

Q1a: How many passengers were on board the Titanic?

 Titanic <- read.csv(paste("Titanic Data.csv", sep=""))
 nrow(Titanic)
[1] 889

Q1b. How many passengers survived the sinking of the Titanic?

Titanic_Survivour= subset(Titanic,Survived == 1)
nrow(Titanic_Survivour)
[1] 340

Q1c. Create a one-way contingency table summarizing the Titanic passengers based on how many survived and how many died.

Titanic$Survived.f <- factor(Titanic$Survived, 
                      levels=c(0,1), 
                      labels=c("Perished","Survived"))
mytable<- with(Titanic, table(Survived.f))
mytable
Survived.f
Perished Survived 
     549      340 

Q1d. What was the percentage of passengers who survived the sinking of the Titanic?

prop.table(mytable)
Survived.f
 Perished  Survived 
0.6175478 0.3824522 

Q2a. Create a two-way contingency table characterising the passengers based on survival and based on the passenger class.

Titanic$Survived.f <- factor(Titanic$Survived, 
                      levels=c(0,1), 
                      labels=c("Perished","Survived"))
mytable<- xtabs(~Survived.f+Pclass,data = Titanic)
mytable
          Pclass
Survived.f   1   2   3
  Perished  80  97 372
  Survived 134  87 119

Q2b. Visualize your table using a Bar plot

Titanic$Survived.f <- factor(Titanic$Survived, 
                      levels=c(0,1), 
                      labels=c("Perished","Survived"))
counts <- table(Titanic$Survived.f, Titanic$Pclass)
barplot(counts, main="Survival by Passenger Class",
  xlab="Passenger Class", col=c("darkblue","red"),
    legend = rownames(counts), beside=TRUE)

plot of chunk unnamed-chunk-6

Q2c. How many first-class passengers survived the sinking of the Titanic?

firstclass_data = subset(Titanic, Pclass==1)
Titanic$Survived.f <- factor(Titanic$Survived, 
                      levels=c(0,1), 
                      labels=c("Perished","Survived"))
mytable<- with(firstclass_data, table(Survived.f))
mytable
Survived.f
Perished Survived 
      80      134 

Q2d. What was the percentage of first-class passengers who survived the sinking of the Titanic?

prop.table(mytable)
Survived.f
 Perished  Survived 
0.3738318 0.6261682 

Q3a. Create a three-way contingency table showing the number of passengers based on the passenger's class, gender and survival.

Titanic$Survived.f <- factor(Titanic$Survived, 
                      levels=c(0,1), 
                      labels=c("Perished","Survived"))
mytable<- xtabs(~Pclass+Sex+Survived.f, data = Titanic)
mytable
, , Survived.f = Perished

      Sex
Pclass female male
     1      3   77
     2      6   91
     3     72  300

, , Survived.f = Survived

      Sex
Pclass female male
     1     89   45
     2     70   17
     3     72   47

Q3b. Express Q3a. in percentages, displaying answers up to two decimal places.

round(ftable(addmargins(prop.table(mytable,c(1,2)),3))*100,digits = 2)
              Survived.f Perished Survived    Sum
Pclass Sex                                       
1      female                3.26    96.74 100.00
       male                 63.11    36.89 100.00
2      female                7.89    92.11 100.00
       male                 84.26    15.74 100.00
3      female               50.00    50.00 100.00
       male                 86.46    13.54 100.00

Visualize your table in Q3b, using a bar plot..

Q3c. How many Females traveling by First-Class survived the sinking of the Titanic?

Titanic$Survived.f <- factor(Titanic$Survived, 
                      levels=c(0,1), 
                      labels=c("Perished","Survived"))
mytable<- xtabs(~Sex+Survived.f, data = firstclass_data)
mytable
        Survived.f
Sex      Perished Survived
  female        3       89
  male         77       45

Q3d. What was the percentage of survivors who were female?

mytable<- xtabs(~Sex, data = Titanic_Survivour)
prop.table(mytable)*100
Sex
  female     male 
67.94118 32.05882 

Visualize your answer in Q3d using a Pie-chart.

pie(mytable)

plot of chunk unnamed-chunk-13

Q3e. What was the percentage of females on board the Titanic who survived?

female_passengers= subset(Titanic,Sex== "female")
mytable<- xtabs(~Survived.f, data = female_passengers)
prop.table(mytable)*100
Survived.f
Perished Survived 
25.96154 74.03846