I wrote a nested for loop in R, but the loop is taking way too long to run. I have two big datasets. For every row in dfA and for every row in dfB, the loop should see if Date in dfA falls within Date Interval in dfB. If this is true, then the two datasets should merge on a given column for that row. I'm not sure if the code I wrote will work w/o error, because the loop is still running.
Any insight would be appreciated.
dfA:
Common a Date
1 20141331123 1 2005-01-01
2 20141331123 2 2005-01-02
3 20141331123 3 2005-01-03
4 20141331123 4 2005-01-04
5 20141331123 5 2005-01-05
6 20141331123 6 2005-01-06
dfB:
cDate bDate common
1 2005-01-01 2005-06-13 20141331123
dfB$Interval <- interval(ymd(dfB$cDate), ymd(dfB$bDate))
library(lubridate)
for (i in 1:nrow(dfA)) {
for (i in 1:nrow(dfB)) {
if (dfA$Date[i] %within% dfB$Interval[i] == TRUE) {
merged <- merge(dfA, dfB, by.x = c("common"), by.y = c("Common"))
}
}
return(merged)
}