4

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
ekad
  • 14,436
  • 26
  • 44
  • 46
C_psy
  • 647
  • 8
  • 22

2 Answers2

4

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
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
2

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))))
Pierre L
  • 28,203
  • 6
  • 47
  • 69