Thanks Jeff.
Stefano, per Jeff's comment, you can replace the line

df1$data_POSIXminus9 <- df1$data_POSIX - lubridate::hours(9)

by

df1$data_POSIXminus9 <- df1$data_POSIX - as.difftime(9,units="hours")

On Mon, Sep 21, 2020 at 8:06 PM Jeff Newmiller <jdnew...@dcn.davis.ca.us> wrote:
>
> The base R as.difftime function is perfectly usable to create this offset 
> without pulling in lubridate.
>
> On September 21, 2020 8:06:51 AM PDT, Eric Berger <ericjber...@gmail.com> 
> wrote:
> >Hi Stefano,
> >If you mean from 9am on one day to 9am on the following day, you can
> >do a trick. Simply subtract 9hrs from each timestamp and then you want
> >midnight to midnight for these adjusted times, which you can get using
> >the method you followed.
> >
> >I googled and found that lubridate::hours() can be used to add or
> >subtract hours from a POSIXct.
> >
> >library(lubridate)
> >
> >day_1 <- as.POSIXct("2020-02-19-00-00", format="%Y-%m-%d-%H-%M",
> >tz="Etc/GMT-1")
> >day_2 <- as.POSIXct("2020-02-24-12-00", format="%Y-%m-%d-%H-%M",
> >tz="Etc/GMT-1")
> >df1 <- data.frame(data_POSIX=seq(day_1, day_2, by="30 min"))
> >df1$hs <- rnorm(nrow(df1), 40, 10)
> >df1$diff[2:nrow(df1)] <- diff(df1$hs)
> >
> >df1$data_POSIXminus9 <- df1$data_POSIX - lubridate::hours(9)
> >df1$dayX <- format(df1$data_POSIXminus9,"%y-%m-%d")
> >df2X <- aggregate(diff ~ dayX, df1, sum)
> >df2X
> >
> >HTH,
> >Eric
> >
> >On Mon, Sep 21, 2020 at 5:30 PM Stefano Sofia
> ><stefano.so...@regione.marche.it> wrote:
> >>
> >> Dear R-list members,
> >> I have semi-hourly snowfall data.
> >> I should sum the semi-hourly increments (only the positive ones, but
> >this is not described in my example) day by day, not from 00 to 24 but
> >from 9 to 9.
> >>
> >> I am able to use the diff function, create a list of days and use the
> >function aggregate, but it works only from 0 to 24. Any suggestion for
> >an efficient way to do it?
> >> Here my code:
> >> day_1 <- as.POSIXct("2020-02-19-00-00", format="%Y-%m-%d-%H-%M",
> >tz="Etc/GMT-1")
> >> day_2 <- as.POSIXct("2020-02-24-12-00", format="%Y-%m-%d-%H-%M",
> >tz="Etc/GMT-1")
> >> df1 <- data.frame(data_POSIX=seq(day_1, day_2, by="30 min"))
> >> df1$hs <- rnorm(nrows(df1), 40, 10)
> >> df1$diff[2:nrow(df1)] <- diff(df1$hs)
> >> df1$day <- format(df$data_POSIX,"%y-%m-%d")
> >> df2 <- aggregate(diff ~ day, df, sum)
> >>
> >> Thank you for your help
> >> Stefano
> >>
> >>          (oo)
> >> --oOO--( )--OOo----------------
> >> Stefano Sofia PhD
> >> Civil Protection - Marche Region
> >> Meteo Section
> >> Snow Section
> >> Via del Colle Ameno 5
> >> 60126 Torrette di Ancona, Ancona
> >> Uff: 071 806 7743
> >> E-mail: stefano.so...@regione.marche.it
> >> ---Oo---------oO----------------
> >>
> >> ________________________________
> >>
> >> AVVISO IMPORTANTE: Questo messaggio di posta elettronica può
> >contenere informazioni confidenziali, pertanto è destinato solo a
> >persone autorizzate alla ricezione. I messaggi di posta elettronica per
> >i client di Regione Marche possono contenere informazioni confidenziali
> >e con privilegi legali. Se non si è il destinatario specificato, non
> >leggere, copiare, inoltrare o archiviare questo messaggio. Se si è
> >ricevuto questo messaggio per errore, inoltrarlo al mittente ed
> >eliminarlo completamente dal sistema del proprio computer. Ai sensi
> >dell’art. 6 della DGR n. 1394/2008 si segnala che, in caso di necessità
> >ed urgenza, la risposta al presente messaggio di posta elettronica può
> >essere visionata da persone estranee al destinatario.
> >> IMPORTANT NOTICE: This e-mail message is intended to be received only
> >by persons entitled to receive the confidential information it may
> >contain. E-mail messages to clients of Regione Marche may contain
> >information that is confidential and legally privileged. Please do not
> >read, copy, forward, or store this message unless you are an intended
> >recipient of it. If you have received this message in error, please
> >forward it to the sender and delete it completely from your computer
> >system.
> >>
> >> --
> >> Questo messaggio  stato analizzato da Libra ESVA ed  risultato non
> >infetto.
> >> This message was scanned by Libra ESVA and is believed to be clean.
> >>
> >>
> >>         [[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.
> >
> >______________________________________________
> >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.
>
> --
> Sent from my phone. Please excuse my brevity.

______________________________________________
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