amathew
amathew

Reputation: 366

Remove the last three characters in a string

I have a data frame with a column for date that has a character mode.

$ Date    : chr  "2014-01-11 17:50:53.000"

However, I want to remove the ".000" that trails most of the four mills values in my variable. I'm not sure if all of them are ".000" as there are over four million observations but I want to remove the period and everything to it's right so that the dates look as follows.

$ Date    : chr  "2014-01-11 17:50:53"

I can do the following to extract just the date, but I need to do something to extract just the time.

> date = c("2014-01-11 17:50:53.000")
> as.Date(date)
[1] "2014-01-11"

How can I do this in R?

Upvotes: 1

Views: 191

Answers (1)

alexwhan
alexwhan

Reputation: 16026

You can extract just the element you want with strftime:

chrDate <- "2014-01-11 17:50:53.000"
strftime(chrDate, "%H:%M:%S")
#[1] "17:50:53"

Upvotes: 1

Related Questions