Re: [R] group consecutive dates in a row

2023-08-11 Thread Stefano Sofia
Thank you for your hints.

All of them have been useful, and you solved my problem.

I understood the role of rle, but I think that for my task its use is not 
fundamental.


I will put more attention on looking for the existing documentation.

Thank you again

Stefano


 (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.so...@regione.marche.it
---Oo-oO



Da: Gabor Grothendieck 
Inviato: lunedì 7 agosto 2023 20:30
A: Stefano Sofia
Cc: r-help@R-project.org
Oggetto: Re: [R] group consecutive dates in a row

It is best to use Date, rather than POSIXct, class if there are no times.

Use the cumsum expression shown to group the dates and then summarize
each group.

We assume that the dates are already sorted in ascending order.

  library(dplyr)

  mydf <- data.frame(date = as.Date(c("2012-02-05", "2012-02-06",
"2012-02-07", "2012-02-13", "2012-02-21")))

  mydf %>%
group_by(grp = cumsum(c(0, diff(date)) > 1)) %>%
summarize(start = first(date), end = last(date)) %>%
ungroup %>%
select(-grp)
  ## # A tibble: 3 × 2
  ##   start  end
  ##
  ## 1 2012-02-05 2012-02-07
  ## 2 2012-02-13 2012-02-13
  ## 3 2012-02-21 2012-02-21

or with only base R:

  smrz <- function(x) with(x, data.frame(start = min(date), end = max(date)))
  do.call("rbind", by(mydf, cumsum(c(0, diff(mydf$date)) > 1), smrz))
  ##startend
  ## 0 2012-02-05 2012-02-07
  ## 1 2012-02-13 2012-02-13
  ## 2 2012-02-21 2012-02-21


On Mon, Aug 7, 2023 at 12:42 PM Stefano Sofia
 wrote:
>
> Dear R users,
>
> I have a data frame with a single column of POSIXct elements, like
>
>
> mydf <- data.frame(data_POSIX=as.POSIXct(c("2012-02-05", "2012-02-06", 
> "2012-02-07", "2012-02-13", "2012-02-21"), format = "%Y-%m-%d", 
> tz="Etc/GMT-1"))
>
>
> I need to transform it in a two-columns data frame where I can get rid of 
> consecutive dates. It should appear like
>
>
> data_POSIX_init data_POSIX_fin
>
> 2012-02-05 2012-02-07
>
> 2012-02-13 NA
>
> 2012-02-21 NA
>
>
> I started with two "while cycles" and so on, but this is not an efficient way 
> to do it.
>
> Could you please give me an hint on how to proceed?
>
>
> Thank you for your precious attention and help
>
> Stefano
>
>
>  (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.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 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@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>  
> https://urlsand.esvalabs.com/?u=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help=a1c37615=997ca565

Re: [R] group consecutive dates in a row

2023-08-07 Thread Gabor Grothendieck
It is best to use Date, rather than POSIXct, class if there are no times.

Use the cumsum expression shown to group the dates and then summarize
each group.

We assume that the dates are already sorted in ascending order.

  library(dplyr)

  mydf <- data.frame(date = as.Date(c("2012-02-05", "2012-02-06",
"2012-02-07", "2012-02-13", "2012-02-21")))

  mydf %>%
group_by(grp = cumsum(c(0, diff(date)) > 1)) %>%
summarize(start = first(date), end = last(date)) %>%
ungroup %>%
select(-grp)
  ## # A tibble: 3 × 2
  ##   start  end
  ##
  ## 1 2012-02-05 2012-02-07
  ## 2 2012-02-13 2012-02-13
  ## 3 2012-02-21 2012-02-21

or with only base R:

  smrz <- function(x) with(x, data.frame(start = min(date), end = max(date)))
  do.call("rbind", by(mydf, cumsum(c(0, diff(mydf$date)) > 1), smrz))
  ##startend
  ## 0 2012-02-05 2012-02-07
  ## 1 2012-02-13 2012-02-13
  ## 2 2012-02-21 2012-02-21


On Mon, Aug 7, 2023 at 12:42 PM Stefano Sofia
 wrote:
>
> Dear R users,
>
> I have a data frame with a single column of POSIXct elements, like
>
>
> mydf <- data.frame(data_POSIX=as.POSIXct(c("2012-02-05", "2012-02-06", 
> "2012-02-07", "2012-02-13", "2012-02-21"), format = "%Y-%m-%d", 
> tz="Etc/GMT-1"))
>
>
> I need to transform it in a two-columns data frame where I can get rid of 
> consecutive dates. It should appear like
>
>
> data_POSIX_init data_POSIX_fin
>
> 2012-02-05 2012-02-07
>
> 2012-02-13 NA
>
> 2012-02-21 NA
>
>
> I started with two "while cycles" and so on, but this is not an efficient way 
> to do it.
>
> Could you please give me an hint on how to proceed?
>
>
> Thank you for your precious attention and help
>
> Stefano
>
>
>  (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.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 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@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.



-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

__
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.


Re: [R] group consecutive dates in a row

2023-08-07 Thread Bert Gunter
Here is another way to obtain the day differences that is the argument
of rle() . It is perhaps more reliable in that it uses methods for
class POSIXct rather than depending on the underlying class structure
and conversion via as.numeric. In theory, the methods won't change or
any changes will be documented, whereas class implementations are
allowed to be fluid and undocumented at the R user level (e.g. the
Help system) afaik. In this case, I don't think that would happen, so
I am merely being pedantic, but I hope I do not offend by making the
point.

But I also wanted to say that the OP could have (imo) looked up the
methodology as I describe below, rather than post here. Or perhaps he
did, but got stymied because he did not know about rle(), a somewhat
esoteric base R function. In which case, the search query "how to find
runs of identical values in an R vector" immediately yielded a hit on
rle(). So my message is: **DO** first try to use R's internal Help
before posting, especially for base R related tasks: it is really a
superb resource (again imo). *

OK, enough sermonizing. Here's what I did. Since the data are POSIXct
class, I went to ?POSIXct and browsed through it until I found
"difftime for time intervals" in the *See Also* section. Following
that link to ?difftime showed me this was what was needed, which is:

difftime(mydf[-1,1], mydf[-nrow(mydf), 1], units = "days")
Time differences in days
[1] 1 1 6 8

Cheers,
Bert

*... and I think it is likely that there are time series related
packages that also could be used, perhaps more immediately, to do what
was requested. But that would require a more diligent search. Though
with new LLM's and generative AI for Help systems becoming available,
the degree of diligence required is rapidly decreasing.


On Mon, Aug 7, 2023 at 9:52 AM Ben Bolker  wrote:
>
> rle(as.numeric(diff(mydf$data_POSIX)))  should get you started, I think?
>
> On 2023-08-07 12:41 p.m., Stefano Sofia wrote:
> > Dear R users,
> >
> > I have a data frame with a single column of POSIXct elements, like
> >
> >
> > mydf <- data.frame(data_POSIX=as.POSIXct(c("2012-02-05", "2012-02-06", 
> > "2012-02-07", "2012-02-13", "2012-02-21"), format = "%Y-%m-%d", 
> > tz="Etc/GMT-1"))
> >
> >
> > I need to transform it in a two-columns data frame where I can get rid of 
> > consecutive dates. It should appear like
> >
> >
> > data_POSIX_init data_POSIX_fin
> >
> > 2012-02-05 2012-02-07
> >
> > 2012-02-13 NA
> >
> > 2012-02-21 NA
> >
> >
> > I started with two "while cycles" and so on, but this is not an efficient 
> > way to do it.
> >
> > Could you please give me an hint on how to proceed?
> >
> >
> > Thank you for your precious attention and help
> >
> > Stefano
> >
> >
> >   (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.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 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@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, 

Re: [R] group consecutive dates in a row

2023-08-07 Thread Ben Bolker

rle(as.numeric(diff(mydf$data_POSIX)))  should get you started, I think?

On 2023-08-07 12:41 p.m., Stefano Sofia wrote:

Dear R users,

I have a data frame with a single column of POSIXct elements, like


mydf <- data.frame(data_POSIX=as.POSIXct(c("2012-02-05", "2012-02-06", "2012-02-07", "2012-02-13", 
"2012-02-21"), format = "%Y-%m-%d", tz="Etc/GMT-1"))


I need to transform it in a two-columns data frame where I can get rid of 
consecutive dates. It should appear like


data_POSIX_init data_POSIX_fin

2012-02-05 2012-02-07

2012-02-13 NA

2012-02-21 NA


I started with two "while cycles" and so on, but this is not an efficient way 
to do it.

Could you please give me an hint on how to proceed?


Thank you for your precious attention and help

Stefano


  (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.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 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@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.


[R] group consecutive dates in a row

2023-08-07 Thread Stefano Sofia
Dear R users,

I have a data frame with a single column of POSIXct elements, like


mydf <- data.frame(data_POSIX=as.POSIXct(c("2012-02-05", "2012-02-06", 
"2012-02-07", "2012-02-13", "2012-02-21"), format = "%Y-%m-%d", tz="Etc/GMT-1"))


I need to transform it in a two-columns data frame where I can get rid of 
consecutive dates. It should appear like


data_POSIX_init data_POSIX_fin

2012-02-05 2012-02-07

2012-02-13 NA

2012-02-21 NA


I started with two "while cycles" and so on, but this is not an efficient way 
to do it.

Could you please give me an hint on how to proceed?


Thank you for your precious attention and help

Stefano


 (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.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 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@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.