No ave
?
frame$z <- with(frame, ave(y,x,FUN=seq_along) )
frame
# x y z
#1 a 3 1
#2 a 3 2
#3 a 3 3
#4 b 2 1
#5 b 2 2
A data.table
version could be something like below (thanks to @mnel):
#library(data.table)
#frame <- as.data.table(frame)
frame[,z := seq_len(.N), by=x]
My original thought was to use:
frame[,z := .SD[,.I], by=x]
where .SD
refers to each subset of the data.table
split by
x
. .I
returns the row numbers for an entire data.table
. So, .SD[,.I]
returns the row numbers within each group. Although, as @mnel points out, this is inefficient compared to the other method as the entire .SD
needs to be loaded into memory for each group to run this calculation.