I have a simple table (included below), where I want to create a third column, call it st_date, where the value on the first row will be a fixed value (say 01/30/2020).
For each subsequent row, I want the value in st_date to be the lagged Date value from the previous row + the value in lengths in terms of business days (not weekends)
Ex: So on row #2, the value should be 01/30/2020 + 7 Working Day = 02/10/2020 On Row #2, the value should be 02/10/2020 + 10 = 02/25/2020
The code to generate the original table is
tmp <- as.data.frame(unclass(rle(t_1$BB_W_D))) %>%
mutate(st_date=df_start_date)
==> df_start_date is my starting date I want to have in row #1
structure(list(lengths = c(1L, 7L, 10L, 6L, 2L, 1L, 2L, 4L, 2L,
4L, 9L, 7L, 5L, 3L, 5L, 8L, 5L, 10L, 10L, 3L, 1L, 2L, 6L, 2L,
1L, 2L, 1L, 2L, 1L, 3L, 1L, 4L, 3L, 13L, 10L, 5L, 1L, 10L, 1L,
6L, 2L, 3L, 1L, 1L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 8L, 12L, 2L,
1L, 3L, 6L, 8L, 10L, 6L, 2L, 1L, 2L, 4L, 2L, 4L, 9L, 7L, 5L,
3L, 5L, 8L, 5L, 10L, 10L, 3L, 1L, 2L, 6L, 2L, 1L, 2L, 1L, 2L,
1L, 3L, 1L, 4L, 3L, 13L, 10L, 5L, 1L, 10L, 1L, 6L, 2L, 3L, 1L,
1L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 8L, 12L, 2L, 1L, 3L, 6L), values = structure(c(NA,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L
), .Label = c("Down", "Up"), class = "factor"), st_date = structure(c(18291,
18291, 18291, 18291, 18291, 18291, 18291, 18291, 18291, 18291,
18291, 18291, 18291, 18291, 18291, 18291, 18291, 18291, 18291,
18291, 18291, 18291, 18291, 18291, 18291, 18291, 18291, 18291,
18291, 18291, 18291, 18291, 18291, 18291, 18291, 18291, 18291,
18291, 18291, 18291, 18291, 18291, 18291, 18291, 18291, 18291,
18291, 18291, 18291, 18291, 18291, 18291, 18291, 18291, 18291,
18291, 18291, 18291, 18291, 18291, 18291, 18291, 18291, 18291,
18291, 18291, 18291, 18291, 18291, 18291, 18291, 18291, 18291,
18291, 18291, 18291, 18291, 18291, 18291, 18291, 18291, 18291,
18291, 18291, 18291, 18291, 18291, 18291, 18291, 18291, 18291,
18291, 18291, 18291, 18291, 18291, 18291, 18291, 18291, 18291,
18291, 18291, 18291, 18291, 18291, 18291, 18291, 18291, 18291,
18291, 18291, 18291, 18291), class = "Date")), class = "data.frame", row.names = c(NA,
-113L))
When I run the next code set,
tmp <- tmp %>%
mutate(st_date=lag(st_date,1)+lengths)
It creates the below, where it is not retaining the value in Row #1 and each subsequent row is now just incremented from the original value of 01/30/2020.
Not sure where the disconnect is here since I have used lag before and it never exhibited this behavior before
structure(list(lengths = c(1L, 7L, 10L, 6L, 2L, 1L, 2L, 4L, 2L,
4L, 9L, 7L, 5L, 3L, 5L, 8L, 5L, 10L, 10L, 3L, 1L, 2L, 6L, 2L,
1L, 2L, 1L, 2L, 1L, 3L, 1L, 4L, 3L, 13L, 10L, 5L, 1L, 10L, 1L,
6L, 2L, 3L, 1L, 1L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 8L, 12L, 2L,
1L, 3L, 6L, 8L, 10L, 6L, 2L, 1L, 2L, 4L, 2L, 4L, 9L, 7L, 5L,
3L, 5L, 8L, 5L, 10L, 10L, 3L, 1L, 2L, 6L, 2L, 1L, 2L, 1L, 2L,
1L, 3L, 1L, 4L, 3L, 13L, 10L, 5L, 1L, 10L, 1L, 6L, 2L, 3L, 1L,
1L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 8L, 12L, 2L, 1L, 3L, 6L), values = structure(c(NA,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L
), .Label = c("Down", "Up"), class = "factor"), st_date = structure(c(NA,
18298, 18301, 18297, 18293, 18292, 18293, 18295, 18293, 18295,
18300, 18298, 18296, 18294, 18296, 18299, 18296, 18301, 18301,
18294, 18292, 18293, 18297, 18293, 18292, 18293, 18292, 18293,
18292, 18294, 18292, 18295, 18294, 18304, 18301, 18296, 18292,
18301, 18292, 18297, 18293, 18294, 18292, 18292, 18294, 18294,
18293, 18293, 18293, 18293, 18293, 18299, 18303, 18293, 18292,
18294, 18297, 18299, 18301, 18297, 18293, 18292, 18293, 18295,
18293, 18295, 18300, 18298, 18296, 18294, 18296, 18299, 18296,
18301, 18301, 18294, 18292, 18293, 18297, 18293, 18292, 18293,
18292, 18293, 18292, 18294, 18292, 18295, 18294, 18304, 18301,
18296, 18292, 18301, 18292, 18297, 18293, 18294, 18292, 18292,
18294, 18294, 18293, 18293, 18293, 18293, 18293, 18299, 18303,
18293, 18292, 18294, 18297), class = "Date")), class = "data.frame", row.names = c(NA,
-113L))