titanic <- read.delim('https://raw.githubusercontent.com/IowaBiostat/data-sets/main/titanic/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 characteristic 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)

temptanic<-titanic
temptanic$Survived[temptanic$Survived=="Died"]<-"Total"

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

class_surv_prop <- prop.table(class_surv, 1)
class_surv_prop[,2]

justsurv_sex <- prop.table(table(titanic$Class, titanic$Sex, titanic$Survived), 1:2)[,,2]
justsurv_sex #x

prop.table(table(titanic$Sex))

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]

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

# 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)

sex_surv <- table(titanic$Sex,titanic$Survived)
sex_surv_prop <- prop.table(sex_surv, 1)

sex_surv_prop[,2]

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

prop.table(table(titanic$Class))

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))
