Margaret Hughes
Margaret Hughes

Reputation: 39

as.POSIXct(%Y-%m-%dT%H:%M:S results in NAs

I have data that has a FixDateTime column (head below) where it is a character

head(df$FixDateTime)
[1] "2017-03-15 15:00:04" "2017-03-16 14:00:48" "2017-03-17 13:00:22"
[4] "2017-03-18 12:00:47" "2017-03-19 11:01:00" "2017-03-20 10:00:47"
class(df$FixDateTime)
[1] "character"

Using the code below I try to convert to as.POSIXct and the resulting column is full of NAs. I know that there are no NAs in my dataset

df$DateTime<-as.POSIXct(df$FixDateTime, format="%Y-%m%-dT%H:%M:%S", tz="MST")
head(df$DateTime)
[1] NA NA NA NA NA NA

I have also run the code in the same way omiting the "T" (with a space instead) and it results in the same thing

I have played with the timezone, and this does not seem to be the issue. I just need a column in the POSIXct format containing date and time.

Upvotes: 0

Views: 40

Answers (1)

Vin&#237;cius F&#233;lix
Vin&#237;cius F&#233;lix

Reputation: 8811

You can use a tidyverse approach

lubridate::ymd_hms("2017-03-17 13:00:22",tz = "MST")

Upvotes: 1

Related Questions