Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract DateTime from a string in R

Tags:

r

One of the columns of my data frame have DateTime and some string characters together. LIke:

"<13>1 2018-04-18T10:29:00.581243+10:00 KOI-QWE-HUJ vmon 2318 - -  Some Description..."

I wish to extract only the DateTime part from it so that I have something like this:

"2018-04-18 10:29:00.581243"

I have tried the below:

as.Date(strptime("<13>1 2018-04-18T10:29:00.581243+10:00 KOI-QWE-HUJ vmon 2318 - - Some Description...", "Date: %Y-%m-%d")) But, this returns NA.

Can anyone please rectify this for me. Thanks in advance.

EDIT: I tried the below to get the Date and Time separately. This works but I need to get them together in a column:

To extract Date:

as.Date(str_extract(x, "[0-9]{4}-[0-9]{2}-[0-9]{2}"), format="%Y-%m-%d")
> [1] "2018-04-18"

To extract Time:

str_extract(x, "[0-9]{2}:[0-9]{2}:[0-9]{2}")
> [1] "10:29:00"
like image 779
HJain Avatar asked Oct 16 '25 14:10

HJain


1 Answers

You are right that you should extract the character form of the datetime first. Here is a method that works with that format. It's just using a regular expression and matching 4 digits, then groups of two digits separated by -, T and : where appropriate. You can then use lubridate::ymd_hms as an alternative to as.Date, since it's a good Swiss army knife at different date formats.

library(stringr)
library(lubridate)
string <- "<13>1 2018-04-18T10:29:00.581243+10:00 KOI-QWE-HUJ vmon 2318 - -  Some Description..."
string %>%
  str_extract("\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}") %>%
  ymd_hms()
#> [1] "2018-04-18 10:29:00 UTC"

Created on 2018-05-02 by the reprex package (v0.2.0).

like image 117
Calum You Avatar answered Oct 18 '25 05:10

Calum You



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!