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 at 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]]
?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 at 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > 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. avg.com
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 enricoschumann.net