如果一天中两小时之间的数据符合条件,则一天的子集数据? [英] subset data for a day if data between two hours of the day meets criteria?

查看:35
本文介绍了如果一天中两小时之间的数据符合条件,则一天的子集数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 R 还很陌生,如果你能帮助解决这个问题,那就太好了,因为我无法在网上找到这个问题的任何答案.这是我的数据框 (DF) 的一部分(以这种格式一直持续到 2008 年)

I’m fairly new to R and it would be great if you could help out with this problem as i havent been able to find any answers to this problem online. This is part of my data frame (DF) (it goes on until 2008 in this format)

Counter Date    Hour    counts
1245    26/05/2006  0   1
1245    26/05/2006  100 0
1245    26/05/2006  200 2
1245    26/05/2006  300 0
1245    26/05/2006  400 5
1245    26/05/2006  500 3
1245    26/05/2006  600 9
1245    26/05/2006  700 10
1245    26/05/2006  800 15

这是我的问题:我需要对我的代码进行子集化,以便在 600 到 2200 小时之间,如果计数超过 0,那么我需要将一整天(000 到 2300)保持在数据集,但如果在指定时间段(600 到 2200)内没有计数,则需要删除全天.我该怎么做?

This is my question: I need to subset my code so that between the hours of 600 and 2200 if there are counts over 0 then I need to keep the whole day (000 to 2300) in the data set, but if there are no counts in the specified time period (600 to 2200) then the whole day needs to be deleted. How can I do this?

我尝试使用以下代码来执行此操作,尽管它只需要 600 到 2200 小时之间的计数数据,我不知道如何让它占用一整天.

I tried to do this with the following piece of code, although it takes ONLY the counts data between 600 and 2200 hours and i can't figure out how to make it take the whole day.

DF2=DF[(DF$hour>=600)&(DF$hour<=2200)&(DF$counts>0),] ##16hr worth of counts from 600 to 2200

然后我将使用以下代码将每小时计数聚合为每日计数的数据进行子集化

I’m then subsetting the data where hourly counts are aggregated into daily counts using the following code

daily=subset(DF2)
    daily$date = as.Date(daily$date, "%m/%d/%Y") 
    agg=aggregate(counts~ date, daily, sum)
town=merge(agg,DF2$counter,all=TRUE) 

非常感谢您提前提供帮助,凯蒂

Thank you so much for your help in advance, Katie

推荐答案

试试这个:

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)

这篇关于如果一天中两小时之间的数据符合条件,则一天的子集数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆