This question already has an answer here:
-
Add index to contiguous runs of equal values
3 answers
I'm looking for a way to create a sequence of numbers ($C) that ascend every time a string changes in($A). This is contingent on a grouping variable ($B).
Example:
A B C
a1 1 1
a1 1 1
a1 1 1
a10 1 2
a10 1 2
a2 1 3
a1 2 1
a20 2 2
a30 2 3
Or with dplyr
df %>% group_by(B) %>% mutate(C = match(A, unique(A)))
# Source: local data frame [9 x 3]
# Groups: B
#
# A B C
# 1 a1 1 1
# 2 a1 1 1
# 3 a1 1 1
# 4 a10 1 2
# 5 a10 1 2
# 6 a2 1 3
# 7 a1 2 1
# 8 a20 2 2
# 9 a30 2 3
With base R
df$C <- with(df, ave(as.character(A), B, FUN=function(x) match(x, unique(x))))
Using the devel version of data.table
, could use the new rleid
function
library(data.table) # v >= 1.9.5
setDT(df)[, C := rleid(A), by = B]
# A B C
# 1: a1 1 1
# 2: a1 1 1
# 3: a1 1 1
# 4: a10 1 2
# 5: a10 1 2
# 6: a2 1 3
# 7: a1 2 1
# 8: a20 2 2
# 9: a30 2 3