2

Hello I need to cast dateTimes character like "20/2/06 11:16:16,683" and having difficulties to do it. I have tried the classic (NO COMMA FOR MILLI):

R) strptime("20/2/06 11:16:16.683", "%d/%m/%y %H:%M:%OS")
[1] "2006-02-20 11:16:16.682"

This fails for the format I have:

R) strptime("20/2/06 11:16:16,683", "%d/%m/%y %H:%M:%OS")
[1] "2006-02-20 11:16:16"

This too:

R) strptime("20/2/06 11:16:16,683", "%d/%m/%y %H:%M,%OS")
[1] NA

Is there a way to do this directly or should I use a gsub like function to replace the , with a . before using strptime.

statquant
  • 13,672
  • 21
  • 91
  • 162

1 Answers1

1

I think you need to do the conversion manually.

Here my suggestion without using gsub, but with scan

 ll <- scan(text = "20/2/06 11:16:16,683",sep=',',what="character")
 as.POSIXct(ll[1],format=("%d/%m/%y %H:%M:%OS"))+ as.numeric(ll[2])/1000

 [1] "2006-02-20 11:16:16.683 CET"
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • Ok I think it cannot be done in one go... I did `myFun <- function(from){strptime(gsub(pattern=",",replacement=".",from), format="%m/%d/%Y %H:%M:%OS")}` – statquant Dec 05 '12 at 17:34