子集两个时间段之间的数据帧 [英] Subset a dataframe between two time periods

查看:15
本文介绍了子集两个时间段之间的数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个示例数据框:

If I have an example dataframe:

Date <- c("05/12/2012 05:17:00", "05/12/2012 06:10:00", "05/12/2012 06:12:00", "05/12/2012 06:14:00", 
      "06/12/2012 05:25:00", "06/12/2012 06:55:00", "06/12/2012 06:19:00", "06/12/2012 08:00:00",
      "07/12/2012 05:00:00", "07/12/2012 05:19:00", "07/12/2012 06:04:00",
      "07/12/2012 06:59:00")
Date <- strptime(Date, "%d/%m/%Y %H:%M")
a <- sample(12)
hour <- as.numeric(format(Date, "%H"))
min <- as.numeric(format(Date, "%M")) / 60
hours_mins <- hour + min

df1 <- data.frame(Date,a,hour, min, hours_mins, stringsAsFactors = FALSE)

我希望能够对我的数据帧进行子集化,只留下 05:15 和 06:15 之间的数据(在任何一天).

I wish to be able to subset my dataframe leaving only data (on any day) between the hours of 05:15 and 06:15.

我将小时和分钟转换为十进制变量,并希望我可以执行以下操作:

I converted the hours and mins to a decimal variable, and hoped I might be able to do something like:

df1[df1$hours_mins >= '5.25' & df1$hours_mins < '6.25']

...但可惜这行不通.有人有什么建议吗?

... but alas this doesn't work. Does anyone have any suggestions?

推荐答案

去掉引号,最后加逗号

df1[df1$hours_mins >= 5.25 &df1$hours_mins <6.25,]

这篇关于子集两个时间段之间的数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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