Update: the first one is solved. The issue was caused by df2 because df2 is not a data.frame.
I have two tricky situations for my data (all data frames are huge).
1) df looks like:
ISO3 CurrencyCode OriginalPrice
USA USD 2.5
USA n/a 3.6
CAN CAD 2.8
...
and df2 is a currency code list looks like
ISO3 CurrencyCode
USA USD
CAN CAD
JAP JPY
...
I tried to find those missing currency codes in df by using df2.
I tried the following code but not working:
setDT(df)[df2, CurrencyCode:= CurrencyCode, on = .(ISO3)]
Another issue is similar but more complex.
df looks like:
ID PayDate CurrencyCode ISO3
1 2016/05/01 EUR FIN
2 2019/01/14 CAD CAN
...
10000 2015/07/31 USD USA
10001 2018/12/07 CAD CAN
df2 looks like:
StartDate EndDate CurrencyCode Rate ISO3
2015/01/01 2015/03/05 CAD 0.75 CAN
2017/05/08 2017/12/31 JPY 0.0091 JAP
....
2019/07/01 2019/08/31 JPY 0.0093 JAP
I want to make df looks like:
ID PayDate CurrencyCode Rate ISO3
1 2016/05/01 EUR 1.06 FIN
2 2019/01/14 CAD 0.85 CAN
...
10000 2015/07/31 USD 1 USA
10001 2018/12/07 CAD 0.75 CAN
and the rate is decided by the PayDate
. The PayDate
should locate between the StartDate
and EndDate
in df2. If there's no enough information from df2, then the rate should be defined by the StartDate
closet to the PayDate
.
This is my code:
setDT(df)[df2, Rate:= Rate, .(date =seq(StartDate, EndDate, by = "day")), by = .(ISO3)]
and again.... I got error.