We are given an assignment during the internship to improving our hands on R language. This webpage deals with answer of task 3 of Day 5 Week 1. Folowing are the question we are suppose to give the answer.

Read the Titanic data set into R.Create a dataframe called “titanic”.

titanic.df<-read.csv(paste("Titanic Data.csv",sep=""))
View(titanic.df)

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

889 were the total number of passengers on board the Titanic

titanictable<- xtabs(~ Survived , data = titanic.df)
addmargins(titanictable)
## Survived
##   0   1 Sum 
## 549 340 889

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

340 passengers who survived the sinking of the Titanic.

titanictable<- xtabs(~ Survived , data = titanic.df)
titanictable
## Survived
##   0   1 
## 549 340

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

38.24522% of passengers who survived the sinking of the Titanic.

titanictable<- xtabs(~ Survived , data = titanic.df)
prop.table(titanictable)*100
## Survived
##        0        1 
## 61.75478 38.24522

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

134 first-class passengers who survived the sinking of the Titanic.

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

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

15.07% of people from 1st class survived from total number of passenger.

titanictable<- xtabs(~ Pclass+Survived , data = titanic.df)
prop.table(titanictable)*100
##       Survived
## Pclass         0         1
##      1  8.998875 15.073116
##      2 10.911136  9.786277
##      3 41.844769 13.385827

62.61% of people survived from total number of 1st class passenger on titanic.

titanictable<- xtabs(~ Pclass+Survived , data = titanic.df)
prop.table(titanictable,1)*100
##       Survived
## Pclass        0        1
##      1 37.38318 62.61682
##      2 52.71739 47.28261
##      3 75.76375 24.23625

Use R to count the number of females from First-Class who survived the sinking of the Titanic There were 89 females of first class who survived the sinking of the Titanic.

titanictable<- xtabs(~ Pclass+Sex+Survived, data = titanic.df)
ftable( titanictable)
##               Survived   0   1
## Pclass Sex                    
## 1      female            3  89
##        male             77  45
## 2      female            6  70
##        male             91  17
## 3      female           72  72
##        male            300  47

Use R to measure the percentage of survivors who were female

67.94% of female who survived among the total number of survivors.

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

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

74.038% of female who survived among the total populaton on Titanic.

titanictable <- xtabs(~ Survived+Sex, data= titanic.df)
prop.table( titanictable,2)*100
##         Sex
## Survived   female     male
##        0 25.96154 81.10919
##        1 74.03846 18.89081

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.

library(vcd)
## Loading required package: grid
titanictable<- xtabs(~ Survived+Sex, data = titanic.df)
chisq.test(titanictable)
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  titanictable
## X-squared = 258.43, df = 1, p-value < 2.2e-16

Since our p<0.05 (here it is very less), means the hypothesis is correct.