anandg112
anandg112

Reputation: 462

removing seconds from date-time column in R

I have a column in this format :

2015-02-20 20:11:20.639794
2015-11-12 20:12:33.055875
2016-04-02 04:43:02

I used strptime in R to create a format to my liking but the problem is that seconds value is still there as zeros (see below). Is there an easy way to remove seconds value from the date-time column? I would prefer not to use another library for this.

2015-02-20 20:11:00
2015-11-12 20:12:00
2016-04-02 04:43:00

data$measured_at <- strptime(data$measured_at,  format = "%Y-%m-%d %H:%M")

Upvotes: 1

Views: 3079

Answers (1)

Rich Pauloo
Rich Pauloo

Reputation: 8392

use base::substr()

Another way to accomplish this, although you'd have to convert the resulting character vector back into a date if you needed it in that format, is to use base::substr().

> temp <- "2015-02-20 20:11:00"
> substr(temp, 1, 16)
[1] "2015-02-20 20:11"

Upvotes: 2

Related Questions