0

I have a certainly stupid question, but here is how looks my data:

A
A
A
B
B
C
D
D
D
D

All I want to do is that :

A 1
A 2
A 3
B 1
B 2
C 1
D 1
D 2
D 3
D 4

I guess it should be with a group by function and an easy line in R, but I don't find words to find the good topic.

Thanks

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
  • Have you checked https://stackoverflow.com/questions/6112803/how-to-create-a-consecutive-index-based-on-a-grouping-variable-in-a-dataframe? – Yach Apr 24 '20 at 13:28

1 Answers1

2

Here is a dplyr solution:

data$numbers <- group_indices(data, letters)

Data:

data <-data.frame(letters= c("A", "A", "A", "B", "B", "C", "D", "D", "D", "D"))
Matt
  • 7,255
  • 2
  • 12
  • 34