I have to datasets, that I want to combine:
Dataset 1: Contains time periods for which "perc" is valid:
set.seed(1)
example_df <- data.frame(ID = rep(1:2, each=2),
start = c(as.Date("2014-01-01"), as.Date("2014-03-05"), as.Date("2014-01-13"), as.Date("2014-03-15")),
end = c(as.Date("2014-03-05"), as.Date("2014-04-12"), as.Date("2014-03-01"), as.Date("2014-04-02")),
perc = rnorm(mean= 30, sd= 10, 4))
Dataset 2: Contains pay for each month:
month_start <- as.Date("2014-01-01") + months(0:3)
month_end <- ceiling_date(month_start, "month") - days(1)
set.seed(1)
example_df2 <- data.frame(month_start, month_end,
ID = rep(1:2, each=4),
pay = rnorm(mean= 2000, sd= 80, 8))
The goal is to calculate pay for each individual for each month based on how much perc they worked. Important is to take into account the valid time periods for perc, which might change within the month.
e.g.:
Jan 2014 for ID 1: Pay = 1949.884 (pay)*23.73546 (perc)/100
because perc is valid for the entire January.
However, for March, perc is 23.73546 until 5th and perc is 31.83643 for the rest of March.
Thus,
Mar 2014 for ID 1: Pay = 1949.884 (pay)*23.73546 (perc)/100/ 31 (days of March)*5 + 1949.884 (pay)*31.83643 (perc)/100/ 31 (days of March)*26