Рассмотрим базовые R reshape
с очисткой и вычислением номера месяца с использованием встроенных констант, month.name и month.abb :
# RESHAPE
rdf <- reshape(df, idvar=c("Adv_Code", "Change_Dt"),
varying=list(names(df)[-1][-1]), v.names="OPN",
times=names(df)[-1][-1], timevar="Month_OPN",
new.row.names=1:1E5, direction="long")
# CALCULATION
final_df <- within(rdf, {
# RETRIEVE MONTH NUMBER FROM MONTH NAME/MONTH ABBREV (e.g., JULY or JUL => 7)
Change_Dt_Num <- sapply(Change_Dt, function(x) max(which(month.name==x), which(month.abb==x)))
# REMOVE THE "_OPN" SUFFIX FROM Month_OPN VALUES
Month_OPN <- sub("_OPN", "", Month_OPN)
# RETRIEVE MONTH NUMBER FROM MONTH NAME/MONTH ABBREV (e.g., JULY or JUL => 7)
Month_OPN_Num <- sapply(Month_OPN, function(x) max(which(month.name==x), which(month.abb==x)))
# CONDITIONALLY ASSIGN "A" AND "B" BY COMPARING BOTH MONTH NUMBERS BEFORE/AFTER APRIL
Adv_Status <- ifelse(Month_OPN_Num < Change_Dt_Num & Month_OPN_Num >= 4, "A",
ifelse(Month_OPN_Num < Change_Dt_Num & Month_OPN_Num < 4, "B", "B"))
# REMOVE HELPER COLUMNS (USED FOR ABOVE CALCULATION ONLY)
rm(Change_Dt_Num, Month_OPN_Num)
})
# RE-ORDER ROWS AND RESET ROW NAMES
final_df <- with(final_df, final_df[order(Adv_Code),])
row.names(final_df) <- NULL
выход
final_df
# Adv_Code Change_Dt Month_OPN OPN Adv_Status
# 1 A198 July April 2 A
# 2 A198 July May 0 A
# 3 A198 July June 0 A
# 4 A198 July July 1 B
# 5 A198 July Aug 2 B
# 6 A198 July Sep 0 B
# 7 A198 July Oct 5 B
# 8 A198 July Nov 0 B
# 9 A198 July Dec 0 B
# 10 A198 July Jan 0 B
# 11 A198 July Feb 0 B
# 12 A198 July March 0 B
# 13 A201 April April 0 B
# 14 A201 April May 0 B
# 15 A201 April June 0 B
# 16 A201 April July 0 B
# 17 A201 April Aug 0 B
# 18 A201 April Sep 0 B
# 19 A201 April Oct 0 B
# 20 A201 April Nov 0 B
# 21 A201 April Dec 0 B
# 22 A201 April Jan 0 B
# 23 A201 April Feb 0 B
# 24 A201 April March 0 B
# 25 S1212 Nov April 0 A
# 26 S1212 Nov May 3 A
# 27 S1212 Nov June 4 A
# 28 S1212 Nov July 0 A
# 29 S1212 Nov Aug 0 A
# 30 S1212 Nov Sep 3 A
# 31 S1212 Nov Oct 0 A
# 32 S1212 Nov Nov 1 B
# 33 S1212 Nov Dec 0 B
# 34 S1212 Nov Jan 0 B
# 35 S1212 Nov Feb 0 B
# 36 S1212 Nov March 0 B
Rextester Demo