0
date product serial_column
20/01/2021 A 1
21/01/2021 A 2
1/10/2020 B 1
2/10/2020 B 2
3/10/2020 B 3
4/10/2020 B 4
3/12/2020 C 1
4/12/2020 C 2
5/12/2020 C 3
29/11/2019 D 1
30/11/2019 D 2

I have a data frame like this (but without the serial_column), I would like to add this column. The data is sorted by date and grouped by products. How could I add the serial_column column? I would like to do it with the package dplyr if it's possible.

Thanks.

Miquel
  • 442
  • 3
  • 14
  • Provide your dataset using `dput(x)`. – Nad Pat Nov 22 '21 at 12:45
  • 3
    `df %>% group_by(product) %>% mutate(serial_column = row_number())` – Ronak Shah Nov 22 '21 at 12:48
  • My data set contains private information. But just as example imagine the iris dataset, I would like to add a new column which goes from 1 to n1 for setosa, from 1 to n2 for versicolor, and for 1 to n3 for virginica. I know how to do it with loops but I'm trying to find a more efficient and clean way. – Miquel Nov 22 '21 at 12:48

0 Answers0