Search code examples
rdplyr

Is there a way to add running to grouped dataframe?


I want have variable row in such a way that for soo1 the row is just 1, for soo1 the row is 2.... Any thoughts?

library(dplyr)

data1 <- tibble(
  "id" = c("soo1", "soo1", "soo2", "soo2", "soo3"),
  "amount" = c(100, 21, 23, 23, 45)
) |> 
  group_by(id) |> 
  mutate(row = row_number())

Solution

  • You could use cur_group_id to add an identifier for the group:

    library(dplyr, warn=FALSE)
    
    tibble(
      "id" = c("soo1", "soo1", "soo2", "soo2", "soo3"),
      "amount" = c(100, 21, 23, 23, 45)
    ) |> 
      group_by(id) |> 
      mutate(row = cur_group_id())
    #> # A tibble: 5 × 3
    #> # Groups:   id [3]
    #>   id    amount   row
    #>   <chr>  <dbl> <int>
    #> 1 soo1     100     1
    #> 2 soo1      21     1
    #> 3 soo2      23     2
    #> 4 soo2      23     2
    #> 5 soo3      45     3