Question

I have a dataframe like this, which covers a month worth of data:

dataframe for month data

And I need to calculate the average Points per month for a unique combination of the 2 IDs. For example, the first row has ID1 1511960 and ID2 2942487 and has 22 points. This unique combination of IDs will occur many times through this month of data. So I need to add the points for this combination of IDs for however many times it occurs throughout the dataframe, and then divide this sum by the number of times this combination occurred; thus getting the average points.

I need to do this for every unique combination of the 2 IDs. So if that first combination I used as an example occurred 14 times in this month, we would have 14 point values summed (the first one was 22) divided by 14.

Hope that makes sense.

Was it helpful?

Solution

Try using aggregate:

#Example data
df <- data.frame(ID1 = round(runif(100,1,10)), ID2 = round(runif(100,1,10)), Points = round(runif(100, 1,50)))

aggregate(Points ~ ID1 + ID2, df, mean)
#   ID1 ID2 Points
# 1   2   1     12
# 2   5   1     31
# 3   7   1     48
# 4  10   1      3
# 5   1   2     20

OTHER TIPS

There is a simple solution using data.table package. Convert your data.frame df to data.table dt.

dt <- as.data.table(df)
dt[, mean(Points), by = list(ID1, ID2)]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top