7

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 ?

ceth
  • 44,198
  • 62
  • 180
  • 289
  • I think this might help you: http://stackoverflow.com/a/4559298/2947592 – wvdz Apr 12 '14 at 09:19
  • possible duplicate of [Calculate the number of weekdays between 2 dates in R](http://stackoverflow.com/questions/5046708/calculate-the-number-of-weekdays-between-2-dates-in-r) – Henrik Apr 12 '14 at 09:21

2 Answers2

9

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)
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • 1
    +1, this solution is locale independent, and thus a better idea than my solution. – Paul Hiemstra Apr 12 '14 at 09:45
  • I need to use it for each row in my data frame. I think I need to convert this code to a function and use transform function. Is it right ? – ceth Apr 12 '14 at 12:10
  • I have updated my question. Will be thankful if you give me any hint. – ceth Apr 12 '14 at 12:31
4

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
Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149