对不同年份的同一时间段进行子集

Subset the same period of time for different years

我有一些时间数据,包括几年(不连续),我想 select 每年的时间间隔相同。例如,我使用这段代码 select 一年的时间段,我怎样才能使它更有效率并在剩下的几年里做?

library(lubridate)
date1 <- as.POSIXct("2004-07-01")
date2 <- as.POSIXct("2004-09-30")
inter<-interval(date1,date2)
subdf<-df[df$time %within% inter,]

正如您提到的 lubridate 包,这对您有用:

subdf <- df[month(df$time) %in% c(7,8,9), ]