The question is as follows. Suppose I have a data frame like this:
item | event | sales |
---|---|---|
1 | A | 130 |
1 | B | 156 |
1 | C | 108 |
2 | B | 150 |
2 | D | 118 |
... | ... | ... |
In this data frame, event A
is first in time, then B, then C and so forth.
I now want an average per item-id combination through time.
This means that for item 1 event A, the average is simply 130. For item 1 and event B, the average should be (130+156)/2 = 143. But for item 2, event B, the average is 150 and for item 2 and event D, the average is (130+118)/2 = 124.
So the outcome should look like this:
item | event | sales |
---|---|---|
1 | A | 130 |
1 | B | 143 |
1 | C | 131.33 |
2 | B | 150 |
2 | D | 124 |
... | ... | ... |
Is this possible without a loop? Can we do this with a group by somehow?
Thanks in advance!