[R] add only the 1st of May with POSIXct

Stefano Sofia @te|@no@@o||@ @end|ng |rom reg|one@m@rche@|t
Sat Jul 6 15:02:48 CEST 2024


Sorry for the delay of my answer.

Enrico and Rui, thank you for your help.

Both solutions worked well, in particular Enrico's hint is very easy to implement.


Thank you

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.sofia using regione.marche.it
---Oo---------oO----------------------------------------


________________________________
Da: Enrico Schumann <es using enricoschumann.net>
Inviato: mercoled� 29 maggio 2024 08:15
A: Stefano Sofia
Cc: r-help using R-project.org
Oggetto: Re: [R] add only the 1st of May with POSIXct

[Non ricevi spesso messaggi di posta elettronica da es using enricoschumann.net. Per informazioni sull'importanza di questo fatto, visita https://aka.ms/LearnAboutSenderIdentification.]

On Tue, 28 May 2024, Stefano Sofia writes:

> Dear R-list users,
>
> From an initial and a final date I create a sequence of days using POSIXct.
>
> If this interval covers all or only in part the months from May to October, I need to get rid of the days from the 2nd of May to the 31st of October:
>
>
> a <- as.POSIXct("2002-11-01", format = "%Y-%m-%d", tz="Etc/GMT-1")
>
> b <- as.POSIXct("2004-06-01", format = "%Y-%m-%d", tz="Etc/GMT-1")
>
> mydf <- data.frame(data_POSIX=seq(as.POSIXct(paste(format(a, "%Y-%m-%d"), "09:00:00", sep=""), format="%Y-%m-%d %H:%M:%S", tz="Etc/GMT-1"), as.POSIXct(paste(format(b, "%Y-%m-%d"), "09:00:00", sep=""), format="%Y-%m-%d %H:%M:%S", tz="Etc/GMT-1"), by="1 day"))
>
>
> If I execute
>
> as.data.frame(mydf[format(mydf$data_POSIX,"%m") %in% c("11", "12", "01", "02", "03", "04"), ])
>
> the interval will be
>
> from 2002-11-01 09:00:00 to 2003-04-30 09:00:00
>
> and from 2003-11-01 09:00:00 to 2004-04-30 09:00:00
>
>
> but I need also 2003-05-01 09:00:00 and 2004-05-01 09:00:00
>
>
> How can I solve this problem?
>
>
> Thank you for your attention and your help
>
> Stefano
>

I think this could be simplified a bit:

    a <- as.POSIXct("2002-11-01 09", format = "%Y-%m-%d %H", tz="Etc/GMT-1")
    b <- as.POSIXct("2004-06-01 09", format = "%Y-%m-%d %H", tz="Etc/GMT-1")

Create your sequence:

    S <- seq(a, b , by = "1 day")
    month.day <- format(S, "%m-%d")

Now subset S for those days that you want:

    S[month.day <= "05-01" | month.day > "10-31"]


--
Enrico Schumann
Lucerne, Switzerland
https://eur02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fenricoschumann.net%2F&data=05%7C02%7Cstefano.sofia%40regione.marche.it%7Cfb03eb5bf2e24b4b76b408dc7fa6b98e%7C295eaa1431a14b09bfe65a338b679f60%7C0%7C0%7C638525601265102204%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=46TUwFWwpLcdtZGARpDkhnuHhgZ546khJI%2FONPeYXe4%3D&reserved=0

________________________________

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.

	[[alternative HTML version deleted]]



More information about the R-help mailing list