baberuth <- subset(baseball, id == "ruthba01")
baberuth$cyear <- baberuth$year - min(baberuth$year) + 1
calculate_cyear <- function(df) {
mutate(df,
cyear = year - min(year),
cpercent = cyear / (max(year) - min(year))
)
}
baseball <- ddply(baseball, .(id), calculate_cyear)
baseball <- subset(baseball, ab >= 25)
model <- function(df) {
lm(rbi / ab ~ cyear, data=df)
}
model(baberuth)
models <- dlply(baseball, .(id), model)
Run the code above in your browser using DataLab