0

I want to label number in the same group.I want to group by date then give number in the same group.

Here is my data sample:

df <- data.frame(id = c("RM125","RM126","RM121","RM120","RM128","RM129","RM131","RM133","RM135","RM132"),
                 date = c(rep("1110101",6),rep("1110102",4)))

Deisre Output like:

ID          Date  label
RM125    1110101      1
RM126    1110101      1
RM121    1110101      1
RM120    1110101      1
RM128    1110101      1
RM129    1110101      1
RM131    1110102      2
RM133    1110102      2
RM135    1110102      2
RM132    1110102      2

I have tried many code like

text text text

df %>% group_by(date) %>% mutate(row_number())
df %>% group_by(date) %>% mutate(group = match(date, unique(date)))

Still can't work.Any tip is appreciated.

leslie
  • 13
  • 2

1 Answers1

0

Have you tried cur_group_id()?

library(dplyr)
df %>%
mutate(group = cur_group_id(), .by = date)
Zhiqiang Wang
  • 6,206
  • 2
  • 13
  • 27