I have a dataset which include the following two columns. They refer to sick leave.
df <- data.frame(BEGIN_DT = c("04/12/2013", "14/01/2013", "31/12/2012", "24/09/2013", "31/12/2013"),
END_DT = c("04/12/2013", "15/01/2013", "04/01/2013", "27/09/2013", "31/12/2013"))
rownames(df) <- c(16:20)
BEGIN_DT END_DT
16 04/12/2013 04/12/2013
17 14/01/2013 15/01/2013
18 31/12/2012 04/01/2013
19 24/09/2013 27/09/2013
20 31/12/2013 31/12/2013
What I am looking to do is to make five additional columns, (Q1, Q2, Q3, Q4, Total) counting sick days taken for each quarter, based on the BEGIN_DT and END_DT values.
For example for row 18:
BEGIN_DT END_DT Q1 Q2 Q3 Q4 Total
31/12/2012 04/01/2013 4 0 0 1 5
I've looked here, and this seems to count the total, but I am really stuck with how to break these into quarters.
calculating number of days between 2 columns of dates in data frame 2-columns-of-dates-in-data-frame
survey <- data.frame(date=c("2012/07/26","2012/07/25"),tx_start=c("2012/01/01","2012/01/01 "))
survey$date_diff <- as.Date(as.character(survey$date), format="%Y/%m/%d")-
as.Date(as.character(survey$tx_start), format="%Y/%m/%d")
survey
currently, I have the following code, but it's only giving me the total number of days in Q1, leaving the rest blank. I'm subtracting the begin date from the end date, however after many many hours over the past week, I'm no closer to (or perhaps further away from) figuring out how to populate the new quarter columns.
sick2$Q1 <- if ("/%m/" < 4) {
as.Date(as.character(sick2$END_DT), format="%d/%m/%Y") -
as.Date(as.character(sick2$BEGIN_DT), format="%d/%m/%Y")
} else { "0" }
sick2$Q2 <- if ("/%m/" > 3 & "/%m/" < 7) {
as.Date(as.character(sick2$END_DT), format="%d/%m/%Y") -
as.Date(as.character(sick2$BEGIN_DT), format="%d/%m/%Y")
} else { "0" }
sick2$Q3 <- if ("/%m/" > 6 & "/%m/" < 10) {
as.Date(as.character(sick2$END_DT), format="%d/%m/%Y") -
as.Date(as.character(sick2$BEGIN_DT), format="%d/%m/%Y")
} else { "0" }
sick2$Q4 <- if ("/%m/" > 9) {
as.Date(as.character(sick2$END_DT), format="%d/%m/%Y") -
as.Date(as.character(sick2$BEGIN_DT), format="%d/%m/%Y")
} else { "0" }
Anyway, if anyone could at least point me in the right direction here, I'd v.v.v. much appreciate it, Thanks, Adrian.