您没有提供示例,因此我们对您的问题感到非常困惑。提供一个可重现的例子通常是有建设性的。即使我承认使用日期类型创建示例有点挑战。
set.seed(1234)
#generate sequence of 25 days hour by hour
x <- Sys.time() + seq(1,by=60*60,length.out=24*25)
hh <- as.POSIXlt(x)$hour
## generate the data.frame
dat <- data.frame(DATE = as.POSIXct(format(x,"%Y-%m-%d")),
HOUR=as.POSIXlt(x)$hour,
HRC = 1:length(x))
## introduce random NA
id <- sample(nrow(dat),10,rep=F)
dat$HRC[id] <- NA
我的解决方案从这里开始;它类似于 Gary 解决方案,我使用的是plyr
包但具有不同的功能。
## I choose 2 dates to subset
min.d <- as.POSIXct('2013-03-01')
max.d <- as.POSIXct('2013-03-15')
dat.s <- subset(dat, DATE >=min.d & DATE <= max.d )
res <- ddply(dat.s, .(HOUR), ## grouping by hour
function(x){
any(is.na(x$HRC)) ## I retuen one HRC at least is NA
})
结果:
res[res$V1,]
HOUR V1
6 5 TRUE
12 11 TRUE
14 13 TRUE
17 16 TRUE
19 18 TRUE
22 21 TRUE