You can use aggregate
ID = c(1, 1, 1, 1, 2, 2, 3)
Emotion = c(2, 4, 6, 4, 1, 1, 8)
df <- data.frame(ID, Emotion)
aggregate(.~ID, data=df, mean)
ID Emotion
1 1 4
2 2 1
3 3 8
sapply
could also be useful (this other solution will give you a vector)
sapply(split(df$Emotion, df$ID), mean)
1 2 3
4 1 8
There are a lot of ways to do it including ddply
from plyr package, data.table package, other combinations of split
and lapply
, dcast
from reshape2 package. See this question for further solutions.