4a. First we need to import the Titanic.csv file into R dataframe:
setwd("F:/Data Analytics for Managerial Applications")
titanic.df <- read.csv(paste("Titanic Data.csv", sep = ""))
View(titanic.df)
4b. To calculate the average age of survivors and those who died:
aggregate(titanic.df$Age, by = list(titanic.df$Survived), mean)
## Group.1 x
## 1 0 30.41530
## 2 1 28.42382
Therefore, the average age of survivors is 28.4 and the average age of those who died is 30.4.
4c. To run a t-test to test the hypothesis that the titanic survivors were younger than the passengers who died. The null hypothesis is - There is no significant difference in age between the titanic survivors and the passengers who died.
t.test(Age ~ Survived, data = titanic.df)
##
## Welch Two Sample t-test
##
## data: Age by Survived
## t = 2.1816, df = 667.56, p-value = 0.02949
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 0.1990628 3.7838912
## sample estimates:
## mean in group 0 mean in group 1
## 30.41530 28.42382
Therefore, since the p-value of 0.029 < 0.05, we reject the null hypothesis and conclude that there is infact a significant difference in age between the titanic survivors and passengers who died.