Pat14
Pat14

Reputation: 29

How can I change `as.POSIXct` into useful format?

I have following data, and would like to format the Date/Time into a Datetime format to do analysis on.

Date    Vt1
02/05/15 14:00  23.611
02/05/15 15:00  28.61
02/05/15 16:00  28.61
02/05/15 17:00  19.608

I used following code:

Test$betterDate<- as.POSIXct(substr(Test$Date,1,14), format = "%d/%m/%Y %H:%M")

And keep getting the error:

Error in `$<-.data.frame`(`*tmp*`, "betterDate", value = numeric(0)) : 
  replacement has 0 rows, data has 3836

Upvotes: 0

Views: 52

Answers (1)

mbiron
mbiron

Reputation: 4231

I believe the issue is that 2 digit years are %y not %Y. This works

as.POSIXct("02/05/15 14:00", format = "%d/%m/%y %H:%M")
[1] "2015-05-02 14:00:00 ART"

You might want to check if the timezone is correct (argument tz of as.POSIXct).

Good luck.

Upvotes: 4

Related Questions