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 http://enricoschumann.net ______________________________________________ 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.