I have the following fake dataset. For a period of time every day (dates) the status (status) of all elements (id) was logged.
df <- data.frame( id = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4),
dates = c("2021-01-01",
"2021-01-02",
"2021-01-03",
"2021-01-04",
"2021-01-05",
"2021-01-01",
"2021-01-02",
"2021-01-03",
"2021-01-04",
"2021-01-05",
"2021-01-01",
"2021-01-02",
"2021-01-03",
"2021-01-04",
"2021-01-05",
"2021-01-01",
"2021-01-02",
"2021-01-03",
"2021-01-04",
"2021-01-05"),
status = c("A", "A", "A", "B", "C",
"A", "A", "B", "C", "C",
"A", "B", "C", "D", "E",
"A", "B", "B", "B", "B")
)
> df
id dates status
1 1 2021-01-01 A
2 1 2021-01-02 A
3 1 2021-01-03 A
4 1 2021-01-04 B
5 1 2021-01-05 C
6 2 2021-01-01 A
7 2 2021-01-02 A
8 2 2021-01-03 B
9 2 2021-01-04 C
10 2 2021-01-05 C
11 3 2021-01-01 A
12 3 2021-01-02 B
13 3 2021-01-03 C
14 3 2021-01-04 D
15 3 2021-01-05 E
16 4 2021-01-01 A
17 4 2021-01-02 B
18 4 2021-01-03 B
19 4 2021-01-04 B
20 4 2021-01-05 B
Unfortunately, in order to save space that dataframe was reduced: If on two subsequent days the status was the same, the second entry was dropped. It is assumed, that the status stays the same until it changes again, so the actual dataset looks like this:
> df %>% group_by(id) %>%
+ mutate(dupl = duplicated(status, 2)) %>%
+ ungroup() %>%
+ filter(dupl == FALSE) %>%
+ select(-dupl)
# A tibble: 13 x 3
id dates status
<dbl> <chr> <chr>
1 1 2021-01-01 A
2 1 2021-01-04 B
3 1 2021-01-05 C
4 2 2021-01-01 A
5 2 2021-01-03 B
6 2 2021-01-04 C
7 3 2021-01-01 A
8 3 2021-01-02 B
9 3 2021-01-03 C
10 3 2021-01-04 D
11 3 2021-01-05 E
12 4 2021-01-01 A
13 4 2021-01-02 B
My question now is: How can I go back to that first (complete) version of the dataset again? The time period is always the same for all ids (2021-01-01 to 2021-01-05)