I need to complete what feels like a relatively simple task, but I'm not really sure where to begin.
I have a data frame(DF1) that has 2 relevant columns. One is an ID and the other is a date. I want to have two new cols that give me the date of the previous event and the date of the next event by group. This will require me to group by ID and arrange by date.
I tried using dplyr and it seemed buggy. What I attempted first:
ID<-c("A","A","B","A","B","B","B","A")
date <- c("1/1/2021"
, "1/5/2021"
, '2/1/2021'
, "1/7/2021"
, "2/2/2021"
, "2/5/2021"
, "2/8/2021"
, "1/9/2021")
DF1 <- data.frame(ID, date)
DF1
ID date
A 1/1/2021
A 1/5/2021
B 2/1/2021
A 1/7/2021
B 2/2/2021
B 2/5/2021
B 2/8/2021
A 1/9/2021
DF2 <- DF1 %>% group_by(ID) %>%arrange(date) %>%
mutate(nextdate= dplyr::lead(date, n = 1, default = NA))
DF2
ID date nextdate
A 1/1/2021 1/5/2021
A 1/5/2021 1/7/2021
A 1/7/2021 1/9/2021
A 1/9/2021 2/1/2021
B 2/1/2021 2/2/2021
B 2/2/2021 2/5/2021
B 2/5/2021 2/8/2021
B 2/8/2021 NA
This gets close, but the 4th row should be na, so it seems like it's dropping the grouping. When I drop the arrange then it gives me something else odd (DF3):
DF3 <- DF1 %>% group_by(ID) %>%
mutate(nextdate= dplyr::lead(date, n = 1, default = NA))
DF3
ID date nextdate
1 A 1/1/2021 1/5/2021
2 A 1/5/2021 2/1/2021
3 B 2/1/2021 1/7/2021
4 A 1/7/2021 2/2/2021
5 B 2/2/2021 2/5/2021
6 B 2/5/2021 2/8/2021
7 B 2/8/2021 1/9/2021
8 A 1/9/2021 NA
I saw a suggestion on a few other posts to try and group by ID then arrange using "order_by" with the mutate command. This is what I get (DF4) when I do that:
DF4 <- DF1 %>% group_by(ID) %>%
mutate(nextdate= dplyr::lead(date, order_by=date, n = 1, default = NA))
DF4
ID date nextdate
1 A 1/1/2021 1/5/2021
2 A 1/5/2021 1/7/2021
3 B 2/1/2021 2/2/2021
4 A 1/7/2021 1/9/2021
5 B 2/2/2021 2/5/2021
6 B 2/5/2021 2/8/2021
7 B 2/8/2021 NA
8 A 1/9/2021 2/1/2021
So, I guess the question is, how can I achieve the following (DF5), perhaps without using dplyr:
DF2
ID date nextdate prevdate
A 1/1/2021 1/5/2021 NA
A 1/5/2021 1/7/2021 1/1/2021
A 1/7/2021 1/9/2021 1/5/2021
A 1/9/2021 NA 1/7/2021
B 2/1/2021 2/2/2021 NA
B 2/2/2021 2/5/2021 2/1/2021
B 2/5/2021 2/8/2021 2/5/2021
B 2/8/2021 NA 2/5/2021