Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a text string 20200821144500 into date and time format in r?

Tags:

datetime

r

I have a date/time column saved like this 20200821144500. How do I convert it to date and time format? I tried as.POSIXct('20200821144500',format="%Y-%m-%d %H:%M:%S") which returned NA. Am I using the wrong format or is there another way to do it?

like image 624
ningningning Avatar asked Nov 29 '25 19:11

ningningning


1 Answers

The main feature here is to tell which is first year month or day etc.. in the string. and to use HMS , because:

  • hms, hm and ms usage is defunct, please use HMS, HM or MS instead. Deprecated in version '1.5.6'.
library(lubridate)

string <- "20200821144500"

parse_date_time(string, "ymd_HMS")

[1] "2020-08-21 14:45:00 UTC"
like image 190
TarJae Avatar answered Dec 02 '25 09:12

TarJae