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

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Wed May 29 13:17:08 CEST 2024


Às 07:01 de 29/05/2024, Stefano Sofia escreveu:
> Thank you Rui for your code.
> 
> I basically understood all your suggestions.
> 
> I am using an old version of R (version 3.6.3, installed in a server I am not allowed to control), and the new pipe operator does not work.
> 
> I tried to run your code without the "|>" operator, but I get an error when I use apply.
> 
> Could you please expand your code without the pipe operator?
> 
> 
> Thank you again for your 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.sofia using regione.marche.it
> ---Oo---------oO----------------------------------------
> 
> 
> ________________________________
> Da: Rui Barradas <ruipbarradas using sapo.pt>
> Inviato: martedì 28 maggio 2024 18:19
> A: Stefano Sofia; 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 ruipbarradas using sapo.pt. Per informazioni sull'importanza di questo fatto, visita https://aka.ms/LearnAboutSenderIdentification.]
> 
> Às 16:23 de 28/05/2024, Stefano Sofia escreveu:
>> 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
>>
>>
>>
>>            (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.
>>
>>        [[alternative HTML version deleted]]
>>
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C02%7Cstefano.sofia%40regione.marche.it%7C0d812d3223344a1508d408dc7f31f657%7C295eaa1431a14b09bfe65a338b679f60%7C0%7C0%7C638525100275684754%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C60000%7C%7C%7C&sdata=ac0Hx9auMSeXgsllDaaimZDFBpSLZ%2B3OeOGQoVvcjxQ%3D&reserved=0
>> PLEASE do read the posting guide https://eur02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C02%7Cstefano.sofia%40regione.marche.it%7C0d812d3223344a1508d408dc7f31f657%7C295eaa1431a14b09bfe65a338b679f60%7C0%7C0%7C638525100275684754%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C60000%7C%7C%7C&sdata=NaHBMLopjjkqygAFPtT8oPv7A0UnYY%2BZGN%2Bp9EGCIqs%3D&reserved=0
>> and provide commented, minimal, self-contained, reproducible code.
> Hello,
> 
> First of all, 'a' and 'b' are already objects of class "POSIXct", you
> don't need to repeat the code creating them when creating mydf.
> 
> As for the question, see the code below.
> 
> 
> 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(a, b, by = "1 day"))
> 
> # get the years from the data
> years <- format(c(a, b), "%Y") |> as.integer()
> # this creates a sequence with all the years
> years <- Reduce(`:`, years)
> 
> # coerce to "Date"
> from <- ISOdate(years, 5L, 2L, tz = "Etc/GMT-1")
> to <- ISOdate(years, 10L, 30L, tz = "Etc/GMT-1")
> 
> # this logical index keeps only the dates between May, 2nd and Nov 1st.
> keep <- data.frame(from, to) |>
>     apply(1L, \(x) x[1L] <= mydf$data_POSIX & mydf$data_POSIX <= x[2L]) |>
>     rowSums() > 0L
> 
> mydf[keep, , drop = FALSE]
> 
> 
> 
> Hope this helps,
> 
> Rui Barradas
> 
> 
> --
> Este e-mail foi analisado pelo software antivírus AVG para verificar a presença de vírus.
> https://eur02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.avg.com%2F&data=05%7C02%7Cstefano.sofia%40regione.marche.it%7C0d812d3223344a1508d408dc7f31f657%7C295eaa1431a14b09bfe65a338b679f60%7C0%7C0%7C638525100275684754%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C60000%7C%7C%7C&sdata=3K8hhk%2FA1JdILJYh2py%2Fe9PX%2FQOsxbmpj7xcurw9wCk%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.
Hello,

Here is the code without the pipe operator.


years <- as.integer(format(c(a, b), "%Y") )
years <- Reduce(`:`, years)
from <- ISOdate(years, 5L, 2L, tz = "Etc/GMT-1")
to <- ISOdate(years, 10L, 30L, tz = "Etc/GMT-1")

keep <- apply(data.frame(from, to), 1L, \(x) x[1L] <= mydf$data_POSIX & 
mydf$data_POSIX <= x[2L])
keep <- rowSums(keep) > 0L

mydf[keep, , drop = FALSE]



R 3.6.3 was a long time ago (2020-02-29). Time to update R?

Hope this helps,

Rui Barradas



More information about the R-help mailing list