#Elena Giasi

Titanic<-read.csv("TitanicData.csv")
View(Titanic)
man=subset(Titanic,Titanic$Survived == 1)
View(man)
dim(Titanic)[1]
## [1] 889
nrow(Titanic)
## [1] 889
survived=subset(Titanic,Titanic$Survived==1)
dim(survived)[1]
## [1] 340
View(survived)

survived=subset(Titanic,Titanic$Survived==1)
dim(survived)
## [1] 340   8
Titanic$Survived<-factor(Titanic$Survived,levels = c(0,1),labels =c("No","Yes") )
library(vcd)
## Loading required package: grid
mytable<-with(Titanic,table(Survived))
mytable
## Survived
##  No Yes 
## 549 340
prop.table(mytable)*100
## Survived
##       No      Yes 
## 61.75478 38.24522
mytable2<-xtabs(~Titanic$Pclass+Titanic$Survived,data=Titanic)
mytable2
##               Titanic$Survived
## Titanic$Pclass  No Yes
##              1  80 134
##              2  97  87
##              3 372 119
(addmargins(mytable2))
##               Titanic$Survived
## Titanic$Pclass  No Yes Sum
##            1    80 134 214
##            2    97  87 184
##            3   372 119 491
##            Sum 549 340 889
barplot(mytable2,main = "Survival by Passenger Class", xlab="Passenger class", ylab = "Frequency", col = c("grey","black"),legend=rownames(mytable2),beside = TRUE)

View(Titanic)
qwe= subset(Titanic,Titanic$Survived=="Yes")
View(qwe)
nrow(qwe)
## [1] 340
survived1=subset(Titanic, Titanic$Survived=="Yes" & Titanic$Pclass==1)
View(survived1)
dim(survived1)[1]
## [1] 134
nrow(survived1)
## [1] 134
prop.table(mytable2)
##               Titanic$Survived
## Titanic$Pclass         No        Yes
##              1 0.08998875 0.15073116
##              2 0.10911136 0.09786277
##              3 0.41844769 0.13385827
mytable3<-xtabs(~Survived+Pclass+Sex, data = Titanic)
mytable3
## , , Sex = female
## 
##         Pclass
## Survived   1   2   3
##      No    3   6  72
##      Yes  89  70  72
## 
## , , Sex = male
## 
##         Pclass
## Survived   1   2   3
##      No   77  91 300
##      Yes  45  17  47
ftable(addmargins(mytable3))
##                 Sex female male Sum
## Survived Pclass                    
## No       1               3   77  80
##          2               6   91  97
##          3              72  300 372
##          Sum            81  468 549
## Yes      1              89   45 134
##          2              70   17  87
##          3              72   47 119
##          Sum           231  109 340
## Sum      1              92  122 214
##          2              76  108 184
##          3             144  347 491
##          Sum           312  577 889
ftable(addmargins(prop.table(mytable3,c(1,2)),3)*100)
##                 Sex     female       male        Sum
## Survived Pclass                                     
## No       1            3.750000  96.250000 100.000000
##          2            6.185567  93.814433 100.000000
##          3           19.354839  80.645161 100.000000
## Yes      1           66.417910  33.582090 100.000000
##          2           80.459770  19.540230 100.000000
##          3           60.504202  39.495798 100.000000
par(mfrow=c(1,2))
mytable2<- xtabs(~Survived+Pclass,data=Titanic,Titanic$Sex=="male")
mytable2
##         Pclass
## Survived   1   2   3
##      No   77  91 300
##      Yes  45  17  47
barplot(mytable2,main = "Males",
ylab = "Nr of passengers",
col = c("gray", "white"),legend=rownames(mytable2),beside = TRUE)


mytable4<- xtabs(~Survived+Pclass,data=Titanic,Titanic$Sex=="female")
mytable4
##         Pclass
## Survived  1  2  3
##      No   3  6 72
##      Yes 89 70 72
barplot(mytable4,main = "Females",
ylab = "Nr of passengers",
col = c("gray", "white"),legend=rownames(mytable4),beside = TRUE)

mytable4
##         Pclass
## Survived  1  2  3
##      No   3  6 72
##      Yes 89 70 72
View(mytable4)

survivors<-xtabs(~Titanic$Survived+Titanic$Sex,data = Titanic)
survivors
##                 Titanic$Sex
## Titanic$Survived female male
##              No      81  468
##              Yes    231  109
chisq.test(survivors)[3]
## $p.value
## [1] 3.77991e-58
sex<-factor(Titanic$Sex,levels = c("female","male"),
            labels = c("F","M"))

tab<-xtabs(~Survived+Pclass+Sex, data = Titanic)
mosaicplot(tab, main = "Mosaic Plot Survors",color = c("green","white","red"))

dead<-subset(Titanic,Titanic$Survived==0)
averagedead<-mean((dead$Age))
averagealived<-mean(survived$Age)
matrix<-matrix(c(averagedead,averagealived))
colnames(matrix)<- "Average"
row.names(matrix)<-c("Dead","Alived")
matrix
##         Average
## Dead        NaN
## Alived 28.42382
boxplot(Titanic$Age~Titanic$Survived,data = Titanic, 
        main="Age of alived and dead",
        xlab="age",
        ylab="alive or dead")