Hi All,
I would like to add rows based on a condition as follows and am not able to achieve it:
I would like to make sure we have all codes if when ACT/FC == "FC", if we see them in data when ACT/FC == "ACT". So, I should see "A_2","A_3","A_4" when ACT/FC is FC for Country CA and Type A in 202310. Values will be NA for those additional rows. But the rest of the structure will be formed by group if exists in ACT of ACT/FC
library(tidyverse)
# Example data
df <- data.frame(
stringsAsFactors = FALSE,
check.names = FALSE,
ACCTDT = c(202309L,202309L,202309L,
202309L,202310L,202309L,202309L,202309L,202310L),
`ACT/FC` = c("ACT","ACT","ACT","ACT",
"FC","ACT","ACT","ACT","FC"),
Country = c("CA", "CA", "CA", "CA", "CA", "CA", "CA", "CA", "CA"),
Type = c("A", "A", "A", "A", "A", "B", "B", "B", "B"),
Code = c("A_1","A_2","A_3","A_4",
"A_1","B_1","B_2","B_3","B_1"),
Sales = c(100L, 90L, 80L, 120L, 110L, 89L, 130L, 96L, 125L)
)