0

My data is like this. df is the name of the dataframe

Status          ArrivalDate                   ClosedDate       
Closed          2015-12-01 04:40:24         2015-12-08  10:12:08
In Progress     2015-12-03 06:40:00
Pending         2015-12-12 08:40:54
Cancelled       2015-12-06 04:40:24         2015-12-18  11:33:50

Status has 4 factors:Closed,Cancelled,In-Progress,Pending In-Progress and Pending don't have ClosedDates

I want to create another column which gives the time difference between ArrivalDate and ClosedDate only if Status is Closed or Cancelled.

This is the code i'm using now:

df$Life[df$Status=="Closed"|df$Status=="Cancelled"]<-difftime(df$Arrival.Date,df$Closed.Date)

And this is the error message I'm getting

Error in as.POSIXlt.character(x, tz, ...) : character string is not in a standard unambiguous format

PS: difftime() works properly when I tried with just two dates. When extended to the whole column it shows the error.

  • Welcome to Stack Overflow. Please provide a reproducible example and expected output. [Have a look at this link](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for further info. – Sotos May 20 '16 at 07:16

1 Answers1

0

df$Life[df$Status=="Closed"|df$Status=="Cancelled"]<-difftime(as.POSIXct(df$Arrival.Date, format='%d-%m-%Y %H:%M:%S'),as.POSIXct(df$Closed.Date, format='%d-%m-%Y %H:%M:%S'))

Assuming your dates are European (DMY) format, if they're American (MYD), just swap the %m and %d.

hd1
  • 33,938
  • 5
  • 80
  • 91
  • The dates are in Date time format : %y-%m-%d %h:%m:%s. Trying your code with format="%y-%m-%d %h:%m:%s" gives the following warning and puts NA in all rows of 'Life' Column :In df$Life[df$Status == "Closed" | df$Status == "Cancelled"] <- difftime(as.Date(df$Arrival.Date, : number of items to replace is not a multiple of replacement length. – pramod kumar May 20 '16 at 07:37
  • From your data, ` 01-12-2015 04:40:24` does not look like it would be the 2015st day of December in year 1 A.D. (for example) -- I'd suggest you reinterpret your data. – hd1 May 20 '16 at 07:54
  • Thanks for the help so far, I'm sorry I made a mistake while typing the question. My date time is of the format I mentioned in the comment, but in any case I don't think this is an issue of format, because it works when I set date times to 2 variables and use difftime(). using as.POSIXct() is still giving the same warning message and NA. When I give ClosedDate values for everything irrespective of Status, my original code works(after using format() to change type). This makes me think its the filtering which is not working. I know that doesn't make sense but thats whats happening. – pramod kumar May 20 '16 at 09:30
  • Could you add a [dput](http://www.inside-r.org/r-doc/base/dput) of your data to your question, please? – hd1 May 20 '16 at 22:24
  • Thanks for the help, when I used POSIXct format instead of date, it started working. – pramod kumar May 24 '16 at 08:44