I have a main table that contains the dates of the main events for each person:
dfMain <- data.frame(last = c("2017-08-01", "2017-08-01", "2017-08-05","2017-09-02","2017-09-02"),
previous = c(NA, NA, "2017-08-01", "2017-08-05", "2017-08-01"),
personid = c(12341, 122345, 12341, 12341, 122345),
diff = c(NA, NA, 4, 28, 32))
(NS on the "previous" and "difference" variables indicate that this person had his first "main equal", that is, no previous dates and time differences)
I also have a secondary table, which consists of a "secondary event" for each person:
dfSecondary <- data.frame(date = c("2017-09-01", "2017-08-30", "2017-08-04", "2017-08-02", "2017-08-02"),
personid = c(122345, 122345, 12341, 122345, 12341))
My question is: what is the best way (due to the amount of my data) for increasing my "dfMain" data frame with the number of unique secondary events between the dates of the main events for each person,
In a dummy example, my goal is to get this table:
Occurances <- c(NA, NA, 2, 0, 3)
dfObjective <- data.frame(dfMain, Occurances)