This can be approached using either specialized packages such as dplyr
which has row_number()
. We need to group by the variable ('alcgp') and create a new column using mutate
.
library(dplyr)
df1 %>%
group_by( alcgp) %>%
mutate(indx= row_number())
Or using ave
from base R
. We group by 'alcgp' and in the FUN
we can specify seq_along
. I used seq_along(alcgp)
as it may not work if the variable is factor
class.
df1$indx <- with(df1, ave(seq_along(alcgp), alcgp, FUN=seq_along))
Another convenient function in splitstackshape
i.e. getanID
library(splitstackshape)
getanID(df1, 'alcgp')