Have a structure long data set. Where unique IDs enter a time span of 2-3 weeks. The data is is in long format. The unique IDs enter the data set at a different time span. I would like to create a unique ID per time span.

df <- data.frame(id = rep(c("1","2","3","1"), each=2),
            counter=c(1,2,1,2,1,2,1,2),
            date_t=rep(seq(c(ISOdate(2021,3,20,9,7)), by = "day", length.out = 2),times=4),
            task=c("A","B","A","B","A","B","A","B"), stringsAsFactors=FALSE)

This the expected output: enter image description here

1

There are 1 best solutions below

0
Peter On

Is this what you are looking for?


library(dplyr)

df1 <- 
  df %>% 
  mutate(id_new = c(0, cumsum(abs(diff(as.numeric(id))))))

df1
#>   id counter              date_t task id_new
#> 1  1       1 2021-03-20 09:07:00    A      0
#> 2  1       2 2021-03-21 09:07:00    B      0
#> 3  2       1 2021-03-20 09:07:00    A      1
#> 4  2       2 2021-03-21 09:07:00    B      1
#> 5  3       1 2021-03-20 09:07:00    A      2
#> 6  3       2 2021-03-21 09:07:00    B      2
#> 7  1       1 2021-03-20 09:07:00    A      4
#> 8  1       2 2021-03-21 09:07:00    B      4

data

df <- data.frame(id = rep(c("1","2","3","1"), each=2),
                 counter=c(1,2,1,2,1,2,1,2),
                 date_t=rep(seq(c(ISOdate(2021,3,20,9,7)), by = "day", length.out = 2),times=4),
                 task=c("A","B","A","B","A","B","A","B"), stringsAsFactors=FALSE)

Created on 2021-04-12 by the reprex package (v2.0.0)