1

I have a data frame with a varying number of group-ids and corresponding events.

Group   DiffDate    eventNumber
58390   0           0
58390   1765        0
24979   0           0
24979   61          0
24979   84          0
24979   1003        0
24979   1095        0

For final evaluation I need the serial number of the events by group, like this:

Group   DiffDate    eventNumber
58390   0           0
58390   1765        1
24979   0           0
24979   61          1
24979   84          2
24979   1003        3
24979   1095        4

Any Ideas?

Filburt
  • 16,951
  • 12
  • 63
  • 111

2 Answers2

0

You can do

library(plyr)
ddply(data,.(Group),function(x) x$eventNumber = 0:(nrow(x)-1))

This is provided that they're all sorted. If they're not, you can just do that in the function in ddply.

Max Candocia
  • 4,094
  • 32
  • 55
0

Try:

library(dplyr)
df %>% group_by(Group) %>% mutate(eventNumber = row_number()-1)
Steven Beaupré
  • 20,785
  • 7
  • 54
  • 76