# Read in data
tips <- read.delim("http://myweb.uiowa.edu/pbreheny/data/tips.txt")

# Mean and SD
mean(tips$TotBill)
sd(tips$TotBill)

# Percentiles
median(tips$TotBill)
IQR(tips$TotBill)
fivenum(tips$TotBill)
quantile(tips$TotBill) ## Same thing
quantile(tips$TotBill, seq(0,1,.1)) ## By tenths

# Subsets
with(tips, mean(TotBill[Time=="Night"]))
with(tips, by(TotBill, Time, mean))
with(tips, by(TotBill, Time, sd))

# Histograms
hist(tips$TotBill, col="gray", border="white", main="", xlab="Total Bill")
require(lattice)
histogram(~TotBill, data=tips, col="gray", border="white", xlab="Total Bill")

# Histograms with conditioning
histogram(~TotBill|Time, data=tips, col="gray", border="white", xlab="Total Bill")
histogram(~TotBill|Time, data=tips, col="gray", border="white", xlab="Total Bill", layout=c(1,2))

# Box plots
boxplot(TotBill~Time, data=tips, ylab="Total bill")
bwplot(TotBill~Time, data=tips, ylab="Total Bill")

# Scatter plots
with(tips, plot(TotBill, Tip, xlab="Total bill"))
xyplot(Tip~TotBill, data=tips, xlab="Total Bill")

# Scatter plots with conditioning
xyplot(Tip~TotBill|Smoker, data=tips, xlab="Total Bill", pch=19)

# Tip rate
TipRate <- with(tips, 100*Tip/TotBill)

# Histogram of tip rate
hist(TipRate, col="gray", border="white", main="")
hist(TipRate, col="gray", border="white", main="", breaks=seq(0,75,5))
