Sorry I am quite new to R, but I have a dataframe with gamelogs for multiple players. I am trying to get the slope coefficient for each player\'s points over all of their games.
You could do
s <- split(gamelogs, gamelogs$name)
vapply(s, function(x) lm(game ~ pts, x)[[1]][2], 1)
# player1 player2
# -0.42857143 0.08241758
or
do.call(rbind, lapply(s, function(x) coef(lm(game ~ pts, x))[2]))
# pts
# player1 -0.42857143
# player2 0.08241758
Or if you want to use dplyr
, you can do
library(dplyr)
models <- group_by(gamelogs, name) %>%
do(mod = lm(game ~ pts, data = .))
cbind(
name = models$name,
do(models, data.frame(slope = coef(.$mod)[2]))
)
# name slope
# 1 player1 -0.42857143
# 2 player2 0.08241758