FlyingPickle
FlyingPickle

Reputation: 1133

Finding days between in R

I have a data frame with the following content

Date        Comments  failure  #ofdays
2014-10-25  abc        0
2014-10-30  def        1
2014-11-10  ghi        0
2014-11-15  lmo        0

and so on....(it has many more rows). I am trying to write R code to achieve populate the #of days column as follows:

Date        Comments  failure  #ofdays
2014-10-25  abc        0         0
2014-10-30  def        1         0 
2014-11-10  ghi        0         10
2014-11-15  lmo        0         15

So, essentially if there is a failure,# of days should reset to 0. If not, it should hold the cumulative # of days since last failure. I have tried

no.of.days<-ifelse(failure==1,0, difftime((Date),lag(Date,1,default=0))+lag(no.of.days,1)

But I get garbage values for output. It gives me 30,000+ days as output. However if I run difftime between two values for testing purposes, I am getting # of days properly. Could one of you take a look and let me know what I am missing? Thanks in advance!

Upvotes: 0

Views: 63

Answers (1)

nicola
nicola

Reputation: 24480

You can try:

ave(as.numeric(df$Date),cumsum(df$failure!=0),FUN=function(x) x-x[1])
#[1]  0  0 11 16

Please note that between 2014-10-30 and 2014-11-10 there are 11 days (and not 10 as indicate in the question).

Data

df<-structure(list(Date = structure(c(16368, 16373, 16384, 16389), class = "Date"), 
    Comments = c("abc", "def", "ghi", "lmo"), failure = c(0L, 
    1L, 0L, 0L)), .Names = c("Date", "Comments", "failure"), row.names = c(NA, 
-4L), class = "data.frame")

Upvotes: 2

Related Questions