My actual dataset is composed of repeated measurements for each id
, where the number of measurements can vary across individuals. A simplified example is:
dat <- data.frame(id = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L))
dat
## id
## 1 1
## 2 1
## 3 1
## 4 1
## 5 1
## 6 1
## 7 2
## 8 2
## 9 3
## 10 3
## 11 3
I am trying to sequentially number the dat
rows by the id
variable. The result should be:
dat
## id s
## 1 1 1
## 2 1 2
## 3 1 3
## 4 1 4
## 5 1 5
## 6 1 6
## 7 2 1
## 8 2 2
## 9 3 1
## 10 3 2
## 11 3 3
How would you do that? I tried to select the last row of each id
by using duplicated()
, but this is probably not the way, since it works with the entire column.