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 can 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 many ways to do this, including ddply from the ddply package, data.table package, other split and lapply , dcast from reshape2 package. See this question for further solutions.
Jilber urbina
source share