0

I need to assign an index value when a value is repeated.

Here is a sample dataset.

df <- data.frame(id = c("A","A","B","C","D","D","D"))

> df
  id
1  A
2  A
3  B
4  C
5  D
6  D
7  D

How can I get that indexing column as below:

> df1
  id index
1  A     1
2  A     2
3  B     1
4  C     1
5  D     1
6  D     2
7  D     3
amisos55
  • 1,913
  • 1
  • 10
  • 21

2 Answers2

3

base R:

df$index <- ave(rep(1L, nrow(df)), df$id, FUN = seq_along)
df
#   id index
# 1  A     1
# 2  A     2
# 3  B     1
# 4  C     1
# 5  D     1
# 6  D     2
# 7  D     3
r2evans
  • 141,215
  • 6
  • 77
  • 149
1

Another option using n() like this:

library(dplyr)
df %>%
  group_by(id) %>%
  mutate(index = 1:n()) %>%
  ungroup()
#> # A tibble: 7 × 2
#>   id    index
#>   <chr> <int>
#> 1 A         1
#> 2 A         2
#> 3 B         1
#> 4 C         1
#> 5 D         1
#> 6 D         2
#> 7 D         3

Created on 2022-09-23 with reprex v2.0.2

Quinten
  • 35,235
  • 5
  • 20
  • 53