Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to extract all the strings before the second colon in r?

I have a time data looks like this:
2024-01-26 19:24:40.0

I want to extract everything before the second colon like this: 2024-01-26 19:24

I tried the code as below:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

a="2024-01-26 19:24:40.0"
b=sub("(^([^:]+:){2}).*$", "\\1",a)

But the result was like this: 2024-01-26 19:24:

So how to write the code in the right way?

>Solution :

Option 1

> sub("(.*\\d+:\\d+):.*", "\\1", a)
[1] "2024-01-26 19:24"

Option 2

> sub(":\\d+\\.\\d+", "", a)
[1] "2024-01-26 19:24"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading