# [R] Cumulates of snowfall within a given interval

PIKAL Petr petr@p|k@| @end|ng |rom prechez@@cz
Fri Jul 30 10:37:14 CEST 2021

```Hi

I would use ?embed function.

nr <- which(rowSums(embed(mydf\$hn, 2))>=80)
mydf[nr,]

But I feel strange that variant 40,50 should be accepted but 0, 90 should not. Both after two consecutive days result in more than 80cm cumulative snow. What about 1,80 how it differs from 0, 81. basically with your constrain zero means NA so if I change all zeroes to NA I get quite close.

> nr <- which(rowSums(embed(mydf\$hn, 2))>=80)
> mydf[nr,]
data_POSIX hn
6  2018-02-06 40
13 2018-02-13 50
> nr <- which(rowSums(embed(mydf\$hn, 4))>=100)
> mydf[nr,]
data_POSIX hn
12 2018-02-12 10
13 2018-02-13 50
> nr <- which(rowSums(embed(mydf\$hn, 2))>=100)
> mydf[nr,]
data_POSIX hn
6 2018-02-06 40
>

Anyway, you need to polish the result a bit as in four consecutive days both 12 and 13 results in higher than 100 cm

Cheers
Petr

> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of Stefano Sofia
> Sent: Friday, July 30, 2021 9:24 AM
> To: r-help mailing list <r-help using r-project.org>
> Subject: [R] Cumulates of snowfall within a given interval
>
> Dear R users,
> I have a data frame with daily snow cumulates (these quantities are known
> as "hn" and are expressed in cm), from the 1st of December to the 30th of
> April, for more than twenty years.
>
> I would need to find days when the sum of a given short interval (I might
> choose two consecutive days, three consecutive days or something like that)
> is higher than a threshold (it might be 80 cm, or 100 cm).
>
> I am trying with rle, but I really struggle to find an efficient algorithm.
> Could somebody help me with some hints?
>
> Stefano
>
>
> init_day <- as.POSIXct("2018-02-01", format="%Y-%m-%d", tz="Etc/GMT-1")
> fin_day <- as.POSIXct("2018-02-20", format="%Y-%m-%d", tz="Etc/GMT-1")
> mydf <- data.frame(data_POSIX=seq(init_day, fin_day, by="1 day"))
> mydf\$hn <- c(30, 0, 10, 50, NA, 40, 70, 0, 0, 0 , NA, 10, 50, 30, 30, 10, 0, 0, 90, 0)
>
> - if I choose a threshold of 100 cm in two days, I should get the 6th of
> February;
> - if I choose a threshold of 80 cm in two days I should get the 6th and the 13th
> of February, but not the 19th of February because this is a single day;
> - f I choose a threshold of 100 cm in four days, I should get the 12th of
> February.
>
>          (oo)
> --oOO--( )--OOo--------------------------------------
> Stefano Sofia PhD
> Civil Protection - Marche Region - Italy
> Meteo Section
> Snow Section
> Via del Colle Ameno 5
> 60126 Torrette di Ancona, Ancona (AN)
> Uff: +39 071 806 7743
> E-mail: stefano.sofia using 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 Libraesva ESG ed  risultato non infetto.
> This message was scanned by Libraesva ESG and is believed to be clean.
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help