I am attempting to merge two data frames on Asset_NM
, GRADE
, and Time_Period
, subsets of the data frames are below.
In the complete data frames, values of Time_Period
range from 0-360, in both data frames. However, when I merge the data frames, I am only returned records with values of Time_Period
that are multiples of three. So for example, instead of values of Time_Period
of 0,1,2,3 etc up to 360 it returns 3,6,9,12 etc up to 360.
ex.df
ENTITY_NM ALM_PORTFOLIO Asset_NM GRADE FINANCIAL_ELEM_ID CF_DATE Time_Period
743 A E Corporate-Domestic A 100 8/15/2017 0
744 A E Corporate-Domestic A 430 9/15/2017 1
745 A E Corporate-Domestic A 430 10/15/2017 2
746 A E Corporate-Domestic A 430 11/15/2017 3
747 A E Corporate-Domestic A 430 12/15/2017 4
748 A E Corporate-Domestic A 430 1/15/2018 5
749 A E Corporate-Domestic A 430 2/15/2018 6
750 A E Corporate-Domestic A 430 3/15/2018 7
751 A E Corporate-Domestic A 430 4/15/2018 8
752 A E Corporate-Domestic A 430 5/15/2018 9
753 A E Corporate-Domestic A 430 6/15/2018 10
754 A E Corporate-Domestic A 210 7/15/2018 11
ex.df2
Asset_NM GRADE Months_Out Aug 2017 Sep 2017 Oct 2017 Time_Period
723 Corporate-Domestic A 2 1% 1% 1% 3
724 Corporate-Domestic A 3 1% 1% 1% 4
725 Corporate-Domestic A 4 1% 1% 1% 5
726 Corporate-Domestic A 5 1% 1% 1% 6
727 Corporate-Domestic A 6 1% 1% 1% 7
728 Corporate-Domestic A 7 1% 1% 1% 8
729 Corporate-Domestic A 8 1% 1% 1% 9
730 Corporate-Domestic A 9 1% 1% 1% 10
731 Corporate-Domestic A 10 1% 1% 1% 11`
exmerge <- merge(x = ex.df, y = ex.df2, by = c('Asset_NM','GRADE','Time_Period'))
Asset_NM GRADE Time_Period ENTITY_NM ALM_PORTFOLIO FINANCIAL_ELEM_ID CASH_FLOW_DATE Months_Out Aug 2017 Sep 2017
1 Corporate-Domestic A 3 A E 430 11/15/2017 2 1% 1%
2 Corporate-Domestic A 6 A E 430 2/15/2018 5 1% 1%
3 Corporate-Domestic A 9 A E 430 5/15/2018 8 1% 1%
Oct 2017
1 1%
2 1%
3 1%`
I am almost certain that the problem has to do with the Time_Period column in ex.df, which was derived to be either 0, during the present month, or 1 for each month out from the present:
`ex.df$Time_Period <- ifelse(ex.df$FINANCIAL_ELEM_ID == 100,0,(as.yearmon(ex.df$CASH_FLOW_DATE, format = "%m/%d/%Y")-FORECASTdate)*12)
`
with
FORECASTdate <- as.yearmon(Sys.Date(),format = "%m/%d/%Y")
The strange part is that when I don't derive Time_Period with the formula above, and use, say:
ex.df$Time_Period <- seq(0,11,1)
I get the expected results, so I really don't know what to make of that. Thanks in advance for your help, this has me stumped.