Я думаю, вы хотите перенести это сообщение на Перекрестная проверка . В любом случае, это решение вашей проблемы через data.table .
library(data.table)
df = data.frame( playerID = c( "A", "B", "A", "C", "A", "B", "B"),
penalty = c( 0, 2, 4, 2, 0, 0, 2 ) )
setDT(df)
df[ , games := .N, by = playerID ]
df[ , mean_penalty := mean( penalty ), by = playerID ]
df[ , `:=` ( rank1 = mean_penalty / sqrt(games),
rank2 = mean_penalty / log(games+1) ) ]
final = unique(df, by = "playerID" )
> final
playerID penalty games mean_penalty rank1 rank2
1: A 0 3 1.333333 0.7698004 0.9617967
2: B 2 3 1.333333 0.7698004 0.9617967
3: C 2 1 2.000000 2.0000000 2.8853901