-2

I have tdateIn, dateOut, and I want to produce the result dateOut - dateIn as new column. My problem is in the format of my dates:

 ## format of my date: 20140424_195711000
 typeof(test$dateIn); typeof(test$dateOut)
 [1] "integer"
 [1] "integer"

I tried everything but I get NA as result or error:

 date <- as.Date(as.character(dateIn), format("%Y%m%d    %H%M%S"))
 > date
 [1] NA 

  as.POSIXct(dateIn, format="%Y%m%d %H%M%S")
  Error in as.POSIXct.default(t, format = "%Y%m%d %H%M%S") : 
  do not know how to convert 't' to class "POSIXct"

I tried to do that directly:

  diff = dateOut - dateIn
  [1] NA
  Warning message:
  In Ops.factor(test$STEP_DATE_TIME_OUT[1], test$STEP_DATE_TIME_IN[1]) :
  - not meaningful for factors

I do not where is the problem? In the underscore? Or in the type of my variables dateIn and dateOut?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Hazem HASAN
  • 1,598
  • 2
  • 21
  • 38
  • 1
    `typeof` doesn't help you. It would report `"integer"` for a `factor` variable. You are usually interested in the `class`. And you should of course specify the correct format string. – Roland Oct 08 '15 at 13:22
  • Please give a [minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). – Jaap Oct 08 '15 at 13:26

1 Answers1

2
x <- factor("20140424_195711000")
typeof(x)
[1] "integer"

As Roland mentioned, the type will show as "integer" when you have factors. The format pattern should also reflect the pattern of the input string, NOT the output that you are looking for.

strptime("20140424_195711000", "%Y%m%d_%H%M%S")
[1] "2014-04-24 19:57:11 EDT"
Pierre L
  • 28,203
  • 6
  • 47
  • 69