I have a data frame for which i need to create a "recommendations" column based on the values of other columns. the code for which is like this :
new_df<-
df1 %>% mutate(
recommendation =
case_when(
optimal_stay_flag == "Too Short" &
hour(Depart) < 18 &
lead(df1$`Max Speed`) < 18 ~ "recommendation 1",
optimal_stay_flag == "Too Short" &
hour(Depart) >= 18 &
lead(df1$`Max Speed`) < 18 ~ "recommendation 2!"
)
) %>% relocate(recommendation, .after = `Max Speed`)
This works how I want it expect for one exception. I would need the output of the first recommendation (recommendation 1) to be lagged and the output of the second recommendation (recommendation 2) to be leading. basically I want the recommendation 1 to be one cell BELOW where it is currently outputting and recommendation 2 to be one cell ABOVE where it is currently outputting.
I have tried using lag() and lead() but don't know how to properly implement it into this code. I am also open to other approaches other than mutate/case_when.
find a sample of my data
df1 <- structure(list(Arrival = structure(c(1663833600, 1663912800,
1664096400, 1664287200, 1664323200, 1664787600, 1665385200, 1665486000,
1665651600, 1665817200, 1666094400, 1666252800, 1666335600, 1666422000,
1666681200, 1667199600, 1667386800, 1667642400, 1667721600, 1667808000
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), Depart = structure(c(1663884000,
1663952400, 1664132400, 1664323199, 1664395200, 1664816400, 1665417600,
1665511200, 1665673200, 1665849600, 1666123200, 1666292400, 1666371600,
1666458000, 1666706400, 1667232000, 1667415600, 1667678400, 1667764800,
1667840400), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
optimal_stay_flag = c("Too Long", "Optimal", "Optimal", "Optimal",
"Too Long", "Optimal", "Optimal", "Too Short", "Too Short",
"Optimal", "Optimal", "Optimal", "Optimal", "Optimal", "Too Short",
"Optimal", "Optimal", "Optimal", "Optimal", "Optimal"), duration_at_port = c(14,
11, 10, 9.99972222222222, 20, 8, 9, 7, 6, 9, 8, 11, 10, 10,
7, 9, 8, 10, 12, 9), `Port Booking Status` = c("Confirmed",
"Confirmed", "Confirmed", "Confirmed", "Confirmed", "Confirmed",
"Confirmed", "Confirmed", "Confirmed", "Confirmed", "Confirmed",
"Confirmed", "Confirmed", "Confirmed", "Confirmed", "Confirmed",
"Confirmed", "Confirmed", "Confirmed", "Confirmed"), Distance = c(212,
81, 668, 743, NA, 1958, 571, 260, 642, 669, 1252, 583, NA,
120, 1097, 552, 732, 1099, NA, NA), `Max Speed` = c(17.67,
16.2, 18.56, 17.69, 0, 17.88, 16.08, 20.87, 19.68, 18.54,
19.41, 17.05, 7.4, 10, 17.98, 15.77, 18.3, 18.26, 7.4, 7.4
)), row.names = c(NA, 20L), class = "data.frame")