Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove seconds from time in R

Tags:

r

posixct

I am trying to remove the seconds from a column of hours in POSIXct format:

#"2016-04-02 10:33:45 COT" "2016-04-02 22:19:24 COT"
#"2016-04-09 17:47:13 COT" "2016-04-13 16:56:23 COT"

x <- structure(c(1459589625, 1459631964, 1460220433, 1460562983),
     class = c("POSIXct", "POSIXt"), tzone = "")

I am trying this but I am not seeing results:

y <- as.POSIXct(x, format = "%d/%m/%Y %H:%M")
like image 357
anitasp Avatar asked Sep 28 '16 19:09

anitasp


3 Answers

No you are giving as.POSIXct a wrong format...

What about using format

datetimes = as.POSIXct(c("2016-04-02 10:33:45 COT", "2016-04-02 22:19:24 COT" ,"2016-04-09 17:47:13 COT", "2016-04-13 16:56:23 COT")    
format(datetimes,format='%Y%m%d %H:%M')

[1] "20160402 10:33" "20160402 22:19" "20160409 17:47" "20160413 16:56"
like image 141
statquant Avatar answered Nov 06 '22 00:11

statquant


you can use round.POSIXt:

round(Sys.time(), units = "mins")
#"2017-01-30 11:20:00 CET"
like image 29
fc9.30 Avatar answered Nov 05 '22 22:11

fc9.30


This is what you have:

x <- structure(c(1459589625, 1459631964, 1460220433, 1460562983),
     class = c("POSIXct", "POSIXt"), tzone = "")

This is what you want:

x <- format(as.POSIXct(x), "%d-%m-%Y %H:%M")
x

[1] "02-04-2016 15:03" "03-04-2016 02:49" "09-04-2016 22:17" "13-04-2016 21:26"
like image 1
Manoj Kumar Avatar answered Nov 05 '22 23:11

Manoj Kumar