R Markdown

This is an R Markdown document showing a detailed analysis of Titanic Dataset.

Reading and viewing the titanic csv dataset in R

setwd("D:/R Internship")
titanic.df<-read.csv(paste("Titanic Data.csv",sep = ""))
View(titanic.df)

Task 3-a: (To count the total number of passengers on board the Titanic)

sum(table(titanic.df))
## [1] 889

Task 3-b: (To count the number of passengers who survived the sinking of the Titanic)

sum(table(titanic.df[titanic.df$Survived==1,]))
## [1] 340

Task 3-c: (To measure the percentage of passengers who survived the sinking of the Titanic)

tit_surv<-table(titanic.df$Survived)
tit_surv                                  
## 
##   0   1 
## 549 340
prop.table(tit_surv)*100 
## 
##        0        1 
## 61.75478 38.24522

Task 3-d: (To count the number of first-class passengers who survived the sinking of the Titanic)

mytable<-xtabs(~Pclass+Survived, data = titanic.df) 
mytable
##       Survived
## Pclass   0   1
##      1  80 134
##      2  97  87
##      3 372 119

Task 3-e: (To measure the percentage of first-class passengers who survived the sinking of the Titanic)

prop.table(mytable,1)*100  
##       Survived
## Pclass        0        1
##      1 37.38318 62.61682
##      2 52.71739 47.28261
##      3 75.76375 24.23625

Task 3-f: (To count the number of females from First-Class who survived the sinking of the Titanic)

mytable1<-xtabs(~Sex+Pclass+Survived, data = titanic.df)   
ftable(mytable1)
##               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

Task 3-g: (To measure the percentage of survivors who were female)

mytable2<-xtabs(~Survived+Sex, data = titanic.df)    #Task 3-g
prop.table(mytable2,1)*100
##         Sex
## Survived   female     male
##        0 14.75410 85.24590
##        1 67.94118 32.05882

Task 3-h: (To measure the percentage of females on board the Titanic who survived

)

prop.table(mytable2,2)*100
##         Sex
## Survived   female     male
##        0 25.96154 81.10919
##        1 74.03846 18.89081

Task 3-i: (Run a Pearson’s Chi-squared test)

chisq.test(mytable2)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  mytable2
## X-squared = 258.43, df = 1, p-value < 2.2e-16