This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
titanic.df <- read.csv(paste("Titanic.csv"))
View(titanic.df)
Number of Passengers :
passengers <- titanic.df[,1]
length(passengers)
## [1] 889
require(dplyr)
## Loading required package: dplyr
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
glimpse(titanic.df)
## Observations: 889
## Variables: 8
## $ Survived <int> 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1,...
## $ Pclass <int> 3, 1, 3, 1, 3, 3, 1, 3, 3, 2, 3, 1, 3, 3, 3, 2, 3, 2,...
## $ Sex <fctr> male, female, female, female, male, male, male, male...
## $ Age <dbl> 22.0, 38.0, 26.0, 35.0, 35.0, 29.7, 54.0, 2.0, 27.0, ...
## $ SibSp <int> 1, 1, 0, 1, 0, 0, 0, 3, 0, 1, 1, 0, 0, 1, 0, 0, 4, 0,...
## $ Parch <int> 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 1, 0, 0, 5, 0, 0, 1, 0,...
## $ Fare <dbl> 7.2500, 71.2833, 7.9250, 53.1000, 8.0500, 8.4583, 51....
## $ Embarked <fctr> S, C, S, S, S, Q, S, S, S, C, S, S, S, S, S, S, Q, S...
Number of Survivors :
survivors <-filter(titanic.df,Survived==1)
length(survivors[,1])
## [1] 340
Percentage of Survivors :
prop.table(table(titanic.df$Survived))
##
## 0 1
## 0.6175478 0.3824522
Number of first class survivors :
firstsurvivors <-filter(survivors,Pclass==1)
length(firstsurvivors[,1])
## [1] 134
Percentage of passengers in first class who survived :
firstclass <-filter(titanic.df,Pclass==1)
prop.table(table(firstclass$Survived))
##
## 0 1
## 0.3738318 0.6261682
Number of Females in First Class who survived :
femalesfirst <- filter(firstsurvivors,Sex=='female')
length(femalesfirst[,1])
## [1] 89
Percentage of Survivors who were female :
prop.table(table(survivors$Sex))
##
## female male
## 0.6794118 0.3205882
Percentage of Females who survived :
females <-filter(titanic.df,Sex=='female')
prop.table(table(females$Survived))
##
## 0 1
## 0.2596154 0.7403846
Pearson Chi Square Test :
mytable <-xtabs(~Sex+Survived,data=titanic.df)
addmargins(mytable)
## Survived
## Sex 0 1 Sum
## female 81 231 312
## male 468 109 577
## Sum 549 340 889
chisq.test(mytable)
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: mytable
## X-squared = 258.43, df = 1, p-value < 2.2e-16
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.