# 
titanic <- read.delim("https://s3.amazonaws.com/pbreheny-data-sets/titanic.txt")
summary(titanic)


# 
table(titanic$Class)


# 
table(titanic$Class,titanic$Survived)


# 
table(titanic$Class, titanic$Survived, titanic$Sex)


# 
class_surv <- table(titanic$Class,titanic$Survived)

class_surv

class_surv[3,2] 
# The 3 indicates the third row and the 2 indicates the second column, 
# so this is the number of 3rd class passengers who survived.

class_surv[4, 1]

class_surv[,2] 
# Note that the column position is left blank. This extracts all rows and 2nd column.
# In the context of the data, this shows us the number of people that survived in each class.

class_surv[3,]
# Note that the row position is left blank. This extracts the 3rd row (3rd class) and all columns.
# In the context of the data, this shows us the number of people who died and survived from the 3rd class.


# 
justSurv <- titanic[titanic$Survived == "Survived",]
# Note that the name of the column you want should be in quotations.
# ie, == "Survived",] is different from == Survived,]. The latter will produce an error code.

# Or if we only want 2nd class travelers.

justSecond <- titanic[titanic$Class == "2nd",]


# 
prop.table(class_surv, 1) # Gives proportions for each class (by row)

prop.table(class_surv, 2) # Gives proportions for the survival groups (by column)


# echo=FALSE

# This is a bit messy, but they're not going to see it anyway.

temptanic<-titanic
temptanic$Class<-as.character(temptanic$Class)
temptanic$Sex<-as.character(temptanic$Sex)
temptanic$Survived<-as.character(temptanic$Survived)
temptanic$Survived[temptanic$Survived=="Died"]<-"Total"

tablerone<-with(temptanic, table(Class,Survived,Sex))
tablerone[,2,]<-tablerone[,1,]+tablerone[,2,]
print(tablerone)


# echo=FALSE
class_surv_prop <- prop.table(class_surv, 1)

cat("Part a", "\n")
class_surv_prop[,2]

cat("Part b", "\n")
justsurv_sex <- prop.table(table(titanic$Class, titanic$Sex, titanic$Survived), 1:2)[,,2]
justsurv_sex #x

cat("Part c", "\n")
prop.table(table(titanic$Sex))

cat("Part d", "\n")

overall <- prop.table(table(titanic$Sex)) #proportion of overall gender

cat("1st : ",signif(sum(justsurv_sex[1,]*overall),3), "\n")
cat("2nd : ",signif(sum(justsurv_sex[2,]*overall),3), "\n")
cat("3rd : ",signif(sum(justsurv_sex[3,]*overall),3), "\n")
cat("Crew: ",signif(sum(justsurv_sex[4,]*overall),3), "\n")


# 
overallSex <- prop.table(table(titanic$Sex))

#First Method
firstclass <- c(141, 62) / c(145, 180) # From table provided

(first_method1 <- weighted.mean(firstclass, overallSex))


#Second Method
classtable <- table(titanic$Sex,titanic$Class,titanic$Survived)
classes <- prop.table(classtable, 1:2)[,,2]

(first_method2 <- weighted.mean(classes[,1], overallSex))


# echo=FALSE
# Table for table purposes
temptanic<-titanic
temptanic$Survived[temptanic$Survived=="Died"] <- "Total"

tablerone <- with(temptanic, table(Sex,Survived,Class))
tablerone[,2,] <- tablerone[,1,] + tablerone[,2,]
print(tablerone)


# echo=FALSE
sex_surv <- table(titanic$Sex,titanic$Survived)
sex_surv_prop <- prop.table(sex_surv, 1)

sex_surv_prop[,2]


# echo=FALSE
justsurv_class <- prop.table(table(titanic$Sex, titanic$Class, titanic$Survived), 1:2)[,,2]
justsurv_class


# echo=FALSE
prop.table(table(titanic$Class))



# echo=FALSE
overall_practice <- prop.table(table(titanic$Class)) #proportion of overall gender

cat("F : ",signif(sum(justsurv_class[1,]*overall_practice),3), "\n")
cat("M : ",signif(sum(justsurv_class[2,]*overall_practice),3), "\n")



# 
overallclass <- prop.table(table(titanic$Class))

#Second Method
practice_table <- table(titanic$Class,titanic$Sex,titanic$Survived)
sexes <- prop.table(practice_table, 1:2)[,,2]

(weighted.mean(sexes[,1], overallclass))
(weighted.mean(sexes[,2], overallclass))


