0

I currently have a dataset looking like:

Person 
A
A
A
B
B
B
C
C
D
D
D

What I would like is to create a new column with

Person    Count
A         1
A         2
A         3
B         1
B         2
B         3
C         1
C         2
D         1
D         2
D         3

I have played around with .SD and .N but can't seem to get it. Does anyone have any suggestions? Thanks!

eddi
  • 49,088
  • 6
  • 104
  • 155
user123276
  • 179
  • 4

1 Answers1

3

Try the following:

df[, count := seq_len(.N), by = Person]
df
    Person count
 1:      A     1
 2:      A     2
 3:      A     3
 4:      B     1
 5:      B     2
 6:      B     3
 7:      C     1
 8:      C     2
 9:      D     1
10:      D     2
11:      D     3
DatamineR
  • 10,428
  • 3
  • 25
  • 45