Hi Stefano

I think either of these does what you need...


1: This gets the interval column as you want it, but utilises the lubridate 
package:

library(lubridate)
mydf$interval = ceiling_date(mydf$data_POSIX, unit="30 minutes”)


2: Alternative in base R is a bit more long winded: convert the date to numeric 
(in seconds), divide by 1800 (seconds in 30min), take the ceiling, and convert 
back.

mydf$interval = as.POSIXct(ceiling(as.numeric(mydf$data_POSIX)/1800)*1800, 
origin="1970-01-01", tz="Etc/GMT-1")


Cheers


> On 2 Dec 2020, at 17:53, Stefano Sofia <stefano.so...@regione.marche.it> 
> wrote:
> 
> init_day <- as.POSIXct("2018-02-01-00-00", format="%Y-%m-%d-%H-%M", 
> tz="Etc/GMT-1")
> fin_day <- as.POSIXct("2018-02-01-02-00", format="%Y-%m-%d-%H-%M", 
> tz="Etc/GMT-1")
> mydf <- data.frame(data_POSIX=seq(init_day, fin_day, by="10 mins"))
> mydf$vmax <- round(rnorm(13, 35, 10))
> mydf$interval <- cut(mydf$data_POSIX, , breaks="30 min")
> means <- aggregate(vmax ~ interval, mydf, mean)


        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to