## Lab 3 R Code

titanic <- read.delim("http://myweb.uiowa.edu/pbreheny/161/data/titanic.txt")
attach(titanic)

#### Tables 

Table1 <- table(Class)
Table1

Props1 <- prop.table(Table1)
Props1
Perc1 <- 100 * Props1
Perc1

Table2 <- table(Sex,Survived)
Table2
Table3 <- table(Sex,Survived,Age)
Table3

prop.table(Table2) ## Overall using Table2 created previously
prop.table(Table2,1) ## Across row in Table2
prop.table(Table2,2) ## Across col in Table2

prop.table(Table3,c(1,3))

#### Bar Charts 
require(lattice)
barchart(Table1)
barchart(table(Survived))
## A simple barchart showing the counts of survivors and non-survivors,

barchart(table(Survived),horizontal=FALSE)
## Changing horizontal from its default to =FALSE will create a vertical barchart

barchart(table(Class,Survived))
## Using "Survived" as a grouping variable for "Class"
barchart(table(Class,Survived),auto.key=TRUE)
## Includes a key indentifying the groups by color

barchart(table(Class,Sex,Survived),auto.key=TRUE)
barchart(table(Class,Sex,Age,Survived),auto.key=TRUE)
barchart(table(Class,Sex,Age,Survived),auto.key=TRUE,scales="free")
#Allows the scale to change across graphs



## Based on the information above, what proportion of all passengers were female?  How about male?
prop_female <- 470/2201
prop_male <- 1731/2201



## Weighted averages
adj_1st <- prop_female*(141/145) + prop_male*(62/180)
adj_2nd <- prop_female*(93/106) + prop_male*(25/179)
adj_3rd <- prop_female*(90/196) + prop_male*(88/510)
adj_crew <- prop_female*(20/23) + prop_male*(192/862)

adj_1st
adj_2nd
adj_3rd
adj_crew


detach(titanic)

