Hilal Cura
Hilal Cura

Reputation: 21

How can i convert my column(character) to POSIXct to create time series?

I want to convert my PM10 dataframe to xts format by using R ; but the type of the first column(date) is character. Therefore, firstly, I need to convert it to POSIXct; but whenever I tried, it runs to NA.

My dates are like this: 2014-01-01 00:00

I tried this code and it didn't work. How can I make it work?

date_index  <- as.POSIXct(bes_yillik[,1],tz ="UTC",format = "%Y-%m-%d %H:%M:%S")

Upvotes: 0

Views: 173

Answers (2)

akrun
akrun

Reputation: 887301

We use anytime without the need for specifying the format

library(anytime)   
anytime("2014-01-01 00:00")
bes_yillik$timefield <- anytime(bes_yillik$timefield)

Upvotes: 0

Sonny
Sonny

Reputation: 3183

Use the lubridate package for datetime stuff:

lubridate::ymd_hm("2014-01-01 00:00")
[1] "2014-01-01 UTC"

bes_yillik$timefield <- lubridate::ymd_hm(bes_yillik$timefield)

Upvotes: 1

Related Questions