Imagine a data table
ID Score
1 10
1 13
1 12
2 10
3 6
3 6
The average for user 1 is 11.67. The average for user 2 is 10. The average for user 3 is 6.
The UniqAverage I am looking for would be ((11.67+10+6)/3) =9.223`
For replication purposes feel free to use...
library(data.table)
df = data.frame( ID=c(1,1,1,2,3,3), Score1=c(10,13,12,10,6,6) )
dt = data.table(df)
A previous question lead to me learning I could do
dt[, mean(Score1), ID][,mean(V1)]
To get this average of averages by ID.
However, what if I only wanted the mean of scores > 10?
So I'd wind up with
ID V1
1 12.5
2 0
3 0
So my final result would be 4.167
Attempts to use which failed.
dt[, mean( which(Score1) > 10 ), ID][,mean(V1)]
dt = data.table(...). No need to start withdf = data.frame(..). - Frankdt[ dt[,Score1>10], mean(Score1), ID]? Why notdt[Score1>10, mean(Score1), ID]- David Arenburg