subset data for a day if data between two hours of the day meets criteria?

Deadly 提交于 2019-12-06 16:56:37

Try this:

TDF <- subset(DF, hour>=600 & hour<=2200)
# get dates where there at least one hour with count data in range
dates <- subset(aggregate(counts~Date,TDF,sum),counts>0)$Date
# get dates where there are no hours with zero count
dates2 <- subset(aggregate(counts~Date,TDF,prod),counts>0)$Date

DF2 <- subset(DF,Date %in% dates)
DF3 <- subset(DF,Date %in% dates2)

plyr is your friend :)

install.packages(plyr)
library(plyr)

ddply(DF, .(Date), function(day) {
   if (sum(day$hour >=600 & day$hour <= 2200) > 0) day
   else subset(day, hour == -1)
})

ddply will group entries in DF by Date, then for every group, if there is an entry with hour between 6000 and 2200, return that day; otherwise return an empty data frame. ddply will then combine all groups into a resulting data frame.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!