0

I'm trying to find difftime for working days only. I want to calculate difftime according to holidayNYSE calendar. When I use the difftime function weekends and holidays are included in the answers, my dataset contaies only data from working days, but when using difftime I have to subtract the non-working days somehow.

A is a vector of 0 and 1, and I want to find the duration of how many days with 0 or 1. Duration for run one are suppose to be 35 and I get 49 (working days from January 1990).

df <- data.frame(Date=(dates), A)


setDT(df)
df <- data.frame(Date=(dates), A)

DF1 <- df[, list(A = unique(A), duration = difftime(max(Date),min(Date), holidayNYSE
(year=setRmetricsOptions(start="1990-01-01", end="2015-31-12")))), by = run]
DF1
     run   A duration

 1:    1    1  49 days
  2:   2    0  22 days
  3:   3    1  35 days
  4:   4    0  27 days
  5:   5    1  14 days
 ---                  
291: 291    1   6 days
292: 292    0  34 days
293: 293    1  10 days
294: 294    0  15 days
295: 295    1  29 days
Frank
  • 66,179
  • 8
  • 96
  • 180
M.O
  • 1
  • 2

1 Answers1

0

An answer to my question without use of difftime:

df <- data.frame(Date=(dates), Value1=bull01)

setDT(df)
df[, run := cumsum(c(1, diff(Value1) !=0))]

duration <- rep(0)
for (i in 1:295){
ind <- which(df$run==i)
a <- df$Date[ind]
duration[i] <- length(a)
}

c <- rep(c(1,0),295)
c <- c[1:295]
df2 <- data.frame(duration, type=c)


> df2
run   duration type
1         35    1
2         17    0
3         25    1
4         20    0
5         10    1
---
291        5    1
292       25    0
293        9    1
294       11    0
295       21    1
M.O
  • 1
  • 2