0

I'm trying to convert a table column (aifstime_local) containing dates which are currently stored as numbers into datetime. see below for table.

    aifstime_utc aifstime_local      lat      lon apparent_t cloud cloud_base_m
1 20190224230000 20190225083000 -12.6099 131.0474       31.8     -         NULL
2 20190224223000 20190225080000 -12.6099 131.0474       30.9     -         NULL
3 20190224220000 20190225073000 -12.6099 131.0474       30.1     -         NULL
4 20190224213000 20190225070000 -12.6099 131.0474       30.2     -         NULL
5 20190224210000 20190225063000 -12.6099 131.0474       30.2     -         NULL
6 20190224203000 20190225060000 -12.6099 131.0474       29.4     -         NULL

I've tried using the anytime function from the anytime package

library(anytime)
MyData$aifstime_local <- anytime(MyData$aifstime_local)

However this only returns the date, and not the datetime as I need, See below

 aifstime_utc aifstime_local      lat      lon apparent_t cloud cloud_base_m
1 20190224230000     2019-02-25 -12.6099 131.0474       31.8     -         NULL
2 20190224223000     2019-02-25 -12.6099 131.0474       30.9     -         NULL
3 20190224220000     2019-02-25 -12.6099 131.0474       30.1     -         NULL
4 20190224213000     2019-02-25 -12.6099 131.0474       30.2     -         NULL
5 20190224210000     2019-02-25 -12.6099 131.0474       30.2     -         NULL
6 20190224203000     2019-02-25 -12.6099 131.0474       29.4     -         NULL

Are there any functions that can help with this task?

alistaire
  • 42,459
  • 4
  • 77
  • 117
Hugoo
  • 11
  • 3

1 Answers1

0

You can use lubridate's ymd_hms :

lubridate::ymd_hms(df$aifstime_utc)

OR in base R use as.POSIXct specifying appropriate format based on the data.

as.POSIXct(as.character(df$aifstime_utc), format = '%Y%m%d%H%M%S', tz = 'UTC')

Note that both of these give time in UTC timezone, if you want it in your local timezone use correct tz argument in ymd_hms and remove tz = 'UTC in as.POSIXct.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213