Use sequence
and rle
on your sorted data.frame
:
my.df <- data.frame(x=c("t1","t2","t2","t3","t1"), y=c(5,2,7,9,6))
# Order by x
my.df = my.df[order(my.df$x), ]
my.df$occ = sequence(rle(as.vector(my.df$x))$lengths)
my.df
# x y occ
# 1 t1 5 1
# 5 t1 6 2
# 2 t2 2 1
# 3 t2 7 2
# 4 t3 9 1
# Uncomment if you want to go back to original row order
# my.df[order(rownames(my.df)), ]
Update: Something I learned today
I had seen, but not used the ave
function. Looks like you can do this without reordering your original data.frame
:
my.df$occ = ave(as.numeric(my.df$x), as.numeric(my.df$x), FUN=seq_along)