[R] change frequency of wind data correctly

Mathew Guilfoyle mrgu|||oy|e @end|ng |rom gm@||@com
Sun Dec 6 21:29:09 CET 2020


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.sofia using 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]]



More information about the R-help mailing list