根据时间范围对行进行子集 [英] Subset rows according to a range of time

查看:51
本文介绍了根据时间范围对行进行子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的数据框

I have a data frame that looks like that

            date_time loc_id node  energy   kgco2 
1 2009-02-27 00:11:08     87  103 0.00000 0.00000 
2 2009-02-27 01:05:05     87  103 7.00000 3.75900 
3 2009-02-27 02:05:05     87  103 6.40039 3.43701 
4 2009-02-27 03:05:05     87  103 4.79883 2.57697 
5 2009-02-27 04:05:05     87  103 4.10156 2.20254 
6 2009-02-27 05:05:05     87  103 2.59961 1.39599

无论如何我可以根据时间范围对其进行子集化,例如,凌晨 2 点到凌晨 5 点.然后我应该得到如下所示的结果:

Is there anyway I can subset it according to range of time, for example, 2am to 5am. I should then get a result that looks like this:

            date_time loc_id node  energy   kgco2  
3 2009-02-27 02:05:05     87  103 6.40039 3.43701 
4 2009-02-27 03:05:05     87  103 4.79883 2.57697 
5 2009-02-27 04:05:05     87  103 4.10156 2.20254 

推荐答案

我会使用 lubridate 包和 hour() 函数来让你的生活更轻松...

I'd use the lubridate package and the hour() function to make your life easier...

require( lubridate )

with( df , df[ hour( date_time ) >= 2 & hour( date_time ) < 5 , ] )

#            date_time loc_id node  energy   kgco2
#3 2009-02-27 02:05:05     87  103 6.40039 3.43701
#4 2009-02-27 03:05:05     87  103 4.79883 2.57697
#5 2009-02-27 04:05:05     87  103 4.10156 2.20254

这篇关于根据时间范围对行进行子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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