The average unknown number of responses per respondent; R

Scenario: I have df "rating" several user attempts when passing the test. Each observation is an attempt with a user ID and rating. Some users may transfer their first attempt; some may take several; they get unlimited attempts. I want to find the average score for each user.

For instance:

userID = c(1:20, sample(1:20, 10, replace = TRUE))
score = c(rnorm(15, mean = 60, sd = 10), rnorm(8, mean = 70, sd = 5), 
rnorm(7, mean = 90, sd = 2))
scores = data.frame(userID, score)

I need a data frame of end results, which is a list of unique user IDs with the average value of all their attempts (regardless of whether they tried once or several times).

Of all the stupid approaches I tried, the last was:

avgScores = aggregate(scores, by=list("userID"), "mean")

: " ". ( ), , , .

+4
3

( ) aggregate :

aggregate(score~userID,scores,mean)

, , :

aggregate(scores,by=list(userID),mean) ## using name and not string

, data.frame, , .

+5
#data.table
library(data.table)
DT<-data.table(scores)
DT[,.(mean_score=mean(score)),by=userID]

#dplyr
library(dplyr)
scores %>%
group_by(userID)%>%
summarise(mean_score=mean(score))
+3

:

library(dplyr)
scores %>% group_by(userID) %>% summarise(mean = mean(score))
+3

All Articles