I really like the simple data.table syntax for this (not to mention speed) ...
# Load package require( data.table ) # Turn data.frame into a data.table dt <- data.table( df ) # Get running count by ID and T dt[ , Index := 1:.N , by = c("ID" , "T") ] # ID T Index #1: A 1 1 #2: A 1 2 #3: A 2 1 #4: A 2 2 #5: B 1 1 #6: B 1 2 #7: B 1 3 #8: B 1 4
.N is an integer equal to the number of lines in each group. Groups are defined by column names in the by argument, so 1:.N gives the vector as long as the group.
As data.table inherits from data.frame any function that accepts the input data.frame , also accepts the input data.table as an input, and you can easily convert back if you want ( df <- data.frame( dt ) )
source share