Round a Posix Date (Posixct) with Base R Functionality

Round a POSIX date (POSIXct) with base R functionality

base has round.POSIXt to do this. Not sure why it doesn't come up with methods.

x <- as.POSIXct(Sys.time())
x
[1] "2012-07-04 10:01:08 BST"
round(x,"mins")
[1] "2012-07-04 10:01:00 BST"
round(x,"hours")
[1] "2012-07-04 10:00:00 BST"
round(x,"days")
[1] "2012-07-04"

Round an POSIXct date up to the next day

Maybe

trunc(x,"days") + 60*60*24

> x <- as.POSIXct(Sys.time())
> x
[1] "2012-08-09 18:40:08 BST"
> trunc(x,"days")+ 60*60*24
[1] "2012-08-10 BST"

Round a POSIX date and time (posixct) to a date relative to a timezone

round will round to the next day once it's past midday, which is why I think you are seeing 2013-03-06. I also have to explicitly set the tz argument in the call to as.POSIXct

Observe:

round( as.POSIXct("2013-03-05 11:00:00" , tz = "EST" ), "day" )
[1] "2013-03-05 EST"

And then once it passes noon:

round( as.POSIXct("2013-03-05 12:00:00" , tz = "EST" ), "day" )
[1] "2013-03-06 EST"

A call to format extracts the day as a character string without the tz argument. So you can get your original result without the timezone

format( round( as.POSIXct("2013-03-05 12:00:00" , tz = "EST" ), "day" ) )
[1] "2013-03-06"

If you want to round any time on that day to that day perhaps what you want instead is trunc?

format(trunc( as.POSIXct("2013-03-05 12:00:00" , tz = "EST" ), "day" ))
[1] "2013-03-05"

R round PosixCT variable in a data table

From ?round.POSIXt:

Value


An object of class "POSIXlt" or "Date".

Which means the result of round on a POSIX object is a POSIXlt object.

Therefore you need to wrap your round function inside as.POSIXct() to get it back to POSIXct

dt[, timestamp2 := as.POSIXct(round(timeStamp, "mins"))]

Is there a method to round date without memory exceed in r?

This should wowrk nice and fast

#sample data
library( data.table )
n = 1000000
set.seed(123)
DT <- data.table( id = 1:n,
timestamp = sample(seq(as.POSIXct('2017/01/01'), as.POSIXct('2020/05/01'), by="5 mins"), replace = TRUE, n) )

#split timestamp to iDate and iTime
DT[, c("date", "time") := IDateTime( timestamp ) ]
#round the iTime
DT[, time_rounded := round( time, units = "hour" )]
#convert iDate and rounded iTime back to posixct (add timezone if needed)
DT[, timestamp_rounded := as.POSIXct( time_rounded, date = date ) ]

possible issue: rounding to 00:00 the next day... you should test this and adjust the date if needed...

Create a regular sequence of date-times (POSIXct) using seq()

There is a seq() method for objects of class "POSIXt" which is the super class of the "POSIXlt" and "POSIXct" classes. As such you don't need to do any conversion.

> now <- Sys.time()
> tseq <- seq(from = now, length.out = 100, by = "mins")
> length(tseq)
[1] 100
> head(tseq)
[1] "2012-01-19 10:52:38 GMT" "2012-01-19 10:53:38 GMT"
[3] "2012-01-19 10:54:38 GMT" "2012-01-19 10:55:38 GMT"
[5] "2012-01-19 10:56:38 GMT" "2012-01-19 10:57:38 GMT"

Round time by X hours in R?

Use the round_date function from the lubridate package. Assuming you had a data.table with a column named date you could do the following:

dt[, date := round_date(date, '2 hours')]

A quick example will give you exactly the results you were looking for:

x <- as.POSIXct("2014-12-28 22:59:00 EDT")
round_date(x, '2 hours')

Vector of dates inconsistent length after rounding in R

?round.POSIXt reveals that in this case, round() returns a POSIXlt object. But data.table doesn't work with those. So just do

dates_form3 <- round(dates_form2,"days")
dates_form3 <- as.POSIXct(dates_form3)
temp$dates_rounded <- dates_form3
length(dates_form3)
length(temp$dates_unrounded)

and you're fine.

How can I sort POSIXct datetime format in R?

Post your code, the error message does not indicate the issue is your object with the class mentioned, but that you provided a non applicable method for the object that happen to have that class in this case.

The issue lays not in the dplyr functionality as seen also in the examples in other replies.

Here an example of both POSIXlt and POSIXct (which both have the class "POSIXct" "POSIXt"). You can sort on both, and both ways.

df <- data.frame(
Date_et_heurePOSIXct = sample(seq(as.POSIXct('2021-08-01'), as.POSIXct('2021-11-09', tz = "UTC"), by = "1 sec"), 5),
Date_et_heurePOSIXlt = sample(seq(as.POSIXlt('2021-08-01'), as.POSIXlt('2021-11-09', tz = "UTC"), by = "1 sec"), 5)
)

df %>% arrange(Date_et_heurePOSIXct)
df %>% arrange(desc(Date_et_heurePOSIXct))
df %>% arrange(Date_et_heurePOSIXlt)
df %>% arrange(desc(Date_et_heurePOSIXlt))

class(df$Date_et_heurePOSIXct)
class(df$Date_et_heurePOSIXlt)


Related Topics



Leave a reply



Submit