I really like the simple syntax of data.table
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 rows in each group. The groups are defined by the column names in the by
argument, so 1:.N
gives a vector as long as the group.
As data.table
inherits from data.frame
any function that takes a data.frame
as input will also take a data.table
as input and you can easily convert back if you wished ( df <- data.frame( dt )
)