# Example 1 - the ddply function
library(plyr)
# Count number of players recorded for each year
bbPerYear = ddply(baseball, "year", "nrow")
qplot(x = year,
y = nrow,
data = bbPerYear,
geom = "line",
ylab = "number of player seasons")
# Example 2 - another example of the ddply function
# Compute mean rbi (batting attempt resulting in runs)
# for all years. Summarize is the apply function, which
# takes as argument a function that computes the rbi mean
bbMod = ddply(baseball,
"year",
summarise,
mean.rbi = mean(rbi, na.rm = TRUE))
qplot(x = year,
y = mean.rbi,
data = bbMod,
geom = "line",
ylab = "mean RBI")
# Example 3 - adding a new variable
# Add a column career.year which measures the number of years
# passed since each player started batting
bbMod2 = ddply(baseball,
"id",
transform,
career.year = year - min(year) + 1)
# Sample a random subset 3000 rows to avoid over-plotting
bbSubset = bbMod2[sample(dim(bbMod2), 3000), ]
qplot(career.year,
rbi, data = bbSubset,
size = I(0.8),
geom = "jitter",
ylab = "RBI",
xlab = "years of playing") +
geom_smooth(color = "red", se = F, size = 1.5)
# Example 4 - the aaply function
dim(ozone)
latitude.mean = aaply(ozone, 1, mean)
longitude.mean = aaply(ozone, 2, mean)
time.mean = aaply(ozone, 3, mean)
longitude = seq(along = longitude.mean)
qplot(x = longitude,
y = longitude.mean,
ylab = "mean ozone level")
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX  