I am working with a dataset containing serial numbers. I would like to add a column that defines if it is the first, second or nth time that the serial number occurs. For example:
serial occurrence
110 1
111 1
112 1
110 2
110 3
>Solution :
You can use the following code:
df <- data.frame(serial = c(110, 111, 112, 110, 110))
library(dplyr)
df %>%
group_by(serial) %>%
mutate(occurence=1:n())
Output:
# A tibble: 5 × 2
# Groups: serial [3]
serial occurence
<dbl> <int>
1 110 1
2 111 1
3 112 1
4 110 2
5 110 3