Hello!
Need help to have correct lag or lead Columns.
Apparently it shows NAs with lag as well as lead. Example column here is Volume2
df <- data.frame(
stringsAsFactors = FALSE,
Date = c(202308,202308,202308,202309,
202309,202309,202310,202310,202310,202311,202311,
202311,202312,202312,202312,202308,202308,202308,
202309,202309,202309,202310,202310,202310,202311,
202311,202311,202312,202312,202312,202401,202401,
202401,202402,202402,202402,202403,202403,202403,
202404,202404,202404,202405,202405,202405,202406,
202406,202406,202407,202407,202407,202408,202408,202408,
202409,202409,202409,202410,202410,202410,202411,
202411,202411,202412,202412,202412),
State = c("Canada","Canada","Canada",
"Canada","Canada","Canada","Canada","Canada","Canada",
"Canada","Canada","Canada","Canada","Canada",
"Canada","USA","USA","USA","USA","USA","USA","USA",
"USA","USA","USA","USA","USA","USA","USA","USA",
"USA","USA","USA","USA","USA","USA","USA","USA","USA",
"USA","USA","USA","USA","USA","USA","USA","USA",
"USA","USA","USA","USA","USA","USA","USA","USA",
"USA","USA","USA","USA","USA","USA","USA","USA",
"USA","USA","USA"),
Category = c("Soft Goods","Hard Goods",
"Hard Goods","Soft Goods","Hard Goods","Hard Goods",
"Soft Goods","Hard Goods","Hard Goods","Soft Goods",
"Hard Goods","Hard Goods","Soft Goods","Hard Goods",
"Hard Goods","Soft Goods","Hard Goods","Hard Goods",
"Soft Goods","Hard Goods","Hard Goods","Soft Goods",
"Hard Goods","Hard Goods","Soft Goods","Hard Goods",
"Hard Goods","Soft Goods","Hard Goods","Hard Goods",
"Soft Goods","Hard Goods","Hard Goods","Soft Goods",
"Hard Goods","Hard Goods","Soft Goods","Hard Goods","Hard Goods",
"Soft Goods","Hard Goods","Hard Goods","Soft Goods",
"Hard Goods","Hard Goods","Soft Goods","Hard Goods",
"Hard Goods","Soft Goods","Hard Goods","Hard Goods",
"Soft Goods","Hard Goods","Hard Goods","Soft Goods",
"Hard Goods","Hard Goods","Soft Goods","Hard Goods",
"Hard Goods","Soft Goods","Hard Goods","Hard Goods",
"Soft Goods","Hard Goods","Hard Goods"),
Type = c("Type A","Type B","Type C",
"Type A","Type B","Type C","Type A","Type B","Type C",
"Type A","Type B","Type C","Type A","Type B",
"Type C","Type A","Type B","Type C","Type A","Type B",
"Type C","Type A","Type B","Type C","Type A","Type B",
"Type C","Type A","Type B","Type C","Type A",
"Type B","Type C","Type A","Type B","Type C","Type A",
"Type B","Type C","Type A","Type B","Type C","Type A",
"Type B","Type C","Type A","Type B","Type C",
"Type A","Type B","Type C","Type A","Type B","Type C",
"Type A","Type B","Type C","Type A","Type B","Type C",
"Type A","Type B","Type C","Type A","Type B","Type C"),
Product = c("Product A","Product B",
"Product C","Product A","Product B","Product C",
"Product A","Product B","Product C","Product A","Product B",
"Product C","Product A","Product B","Product C",
"Product A","Product B","Product C","Product A","Product B",
"Product C","Product A","Product B","Product C",
"Product A","Product B","Product C","Product A",
"Product B","Product C","Product A","Product B","Product C",
"Product A","Product B","Product C","Product A",
"Product B","Product C","Product A","Product B","Product C",
"Product A","Product B","Product C","Product A",
"Product B","Product C","Product A","Product B",
"Product C","Product A","Product B","Product C","Product A",
"Product B","Product C","Product A","Product B",
"Product C","Product A","Product B","Product C",
"Product A","Product B","Product C"),
Volume = c(60,0,2,100,0,5,80,0,5,
75,0,0,100,0,4,15,0,1,0,0,1,10,0,1,0,0,1,
0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,6,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0),
Rate = c(0.0425,0,0.0185,0.0425,0,
0.0185,0.0425,0,0.0185,0.0425,0,0.0185,0.0425,0,
0.0185,0.0180833333333333,0,0.0141666666666667,
0.0180833333333333,0,0.0141666666666667,0.0180833333333333,0,
0.0141666666666667,0.0180833333333333,0,
0.0141666666666667,0.0180833333333333,0,0.0141666666666667,
0.0180833333333333,0,0.0141666666666667,0.0180833333333333,
0,0.0141666666666667,0.0180833333333333,0,
0.0141666666666667,0.0180833333333333,0,0.0141666666666667,
0.0180833333333333,0,0.0141666666666667,0.0180833333333333,
0,0.0141666666666667,0.0180833333333333,0,
0.0141666666666667,0.0180833333333333,0,0.0141666666666667,
0.0180833333333333,0,0.0141666666666667,0.0180833333333333,
0,0.0141666666666667,0.0180833333333333,0,
0.0141666666666667,0.0180833333333333,0,0.0141666666666667)
)
df1 <- df %>%
mutate(Volume1 = Volume * (1-Rate)) %>%
group_by(Date, State, Category, Type, Product) %>%
mutate(Volume2 = lead(Volume1))