Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between two dates excluding weekends

Tags:

r

I have two date columns in my date frame. I can find difference between this dates using:

issues <- transform(issues, duration = difftime(strptime(close_date, format="%d.%m.%Y"), 
                                                strptime(created_on, format = "%d.%m.%Y"), units="days"))

Is there any way to find duration of issues excluding weekends (Saturdays and Sundays) ?

Update

I have tried to use use @agstudy solution:

getDuration <- function(d1, d2) {  
  myDays <- seq.Date(to = as.Date(d2, format="%d.%m.%Y"), 
                     from = as.Date(d1, format = "%d.%m.%Y"), by=1)
  result <- length(myDays[!is.weekend(myDays)])
  return(result)
}

issues <- transform(issues, duration = getDuration(created_on, close_date))

But get the error:

Error in seq.Date(to = as.Date(d2, format = "%d.%m.%Y"), from = as.Date(d1,  : 
  'from' must be length 1 

Why ?

like image 263
ceth Avatar asked Apr 12 '14 09:04

ceth


2 Answers

Another option is to create a dates sequence, exclude weekends and to compute its length.

library(chron)
length(myDays[!is.weekend(myDays)])

Here an example:

library(chron)
myDays <- 
seq.Date(to = as.Date('01.05.2014', format="%d.%m.%Y"), 
         from=as.Date('01.01.2014', format = "%d.%m.%Y"),by=1)
length(myDays)
length(myDays[!is.weekend(myDays)])

EDIT

You should vectorize your function in order to use it with vectors.

getDuration <- function(d1, d2,fmt="%d.%m.%Y") {  
  myDays <- seq.Date(to   = as.Date(d2, format=fmt), 
                     from = as.Date(d1, format =fmt), 
                     by   = 1)
  length(myDays[!is.weekend(myDays)]
}

Here I am using mapply:

mapply(getDuration ,issues$created_on,issues$close_date)
like image 60
agstudy Avatar answered Sep 19 '22 18:09

agstudy


First a function to determine the amount of weekend days between two dates:

no_weekend_days = function(start_date, stop_date) {
    vector_with_days = strftime(seq(start, stop, by = 24 * 3600), '%A')
    return(sum(vector_with_days %in% c('Saturday', 'Sunday')))
}

And an example that uses the function:

start = as.POSIXct('2014-04-10')
stop = as.POSIXct('2014-04-21')
difftime(stop, start)
# > Time difference of 11 days
difftime(stop, start) - no_weekend_days(start, stop)
# > Time difference of 7 days
like image 44
Paul Hiemstra Avatar answered Sep 21 '22 18:09

Paul Hiemstra