用小时过滤时间戳的最佳方法

时间:2018-08-23 13:30:07

标签: r lubridate

structure(list(id = c(14735, 11589, 1165, 7864, 9151, 6662, 26, 6638, 7635, 10204, 10588, 11923, 2119, 2487, 11571, 6759, 9591, 
1592, 12725, 5086, 3039, 10576, 2526, 1127, 583, 12879, 5686, 13405, 1375, 7547, 11479, 9220, 8040, 13848, 14996, 4256, 1879, 
2653, 15220, 1896, 4547, 2505, 1105, 3625, 10896, 9806, 1154, 2626, 2215, 5957, 3522, 8531, 8867, 1501, 2415, 14009, 13056, 
13740, 1751, 540, 8896, 4771, 8457, 5383, 2176, 8611, 5072, 1828, 3884, 5364, 12617, 11887, 14267, 12735, 2261, 8962, 12501, 9586, 
7129, 3925, 373, 4987, 3410, 13304, 10276, 7975, 8456, 3752, 111, 14384, 10901, 4234, 11273, 13196, 5764, 10902, 3631, 9814, 
14781, 5726), full_date = structure(c(1522781128, 1522108662, 1519981076, 1521121363, 1521457099, 1520859176, 1519631141, 1520856439, 
1521056830, 1521753388, 1521853223, 1522173544, 1520160750, 1520238731, 1522105027, 1520873428, 1521581248, 1520074992, 1522326287, 1520600253, 
1520300281, 1521850210, 1520242830, 1519972727, 1519776890, 1522350676, 1520695451, 1522446321, 1520025013, 1521042071, 1522085537, 1521481296, 
1521154393, 1522521269, 1522939333, 1520464381, 1520117285, 1520254658, 1523208654, 1520119807, 1520509952, 1520241232, 1519954606, 1520375033, 
1521937117, 1521645125, 1519978700, 1520252273, 1520176769, 1520750012, 1520362171, 1521290786, 1521377710, 1520061816, 1520221068, 1522546134, 
1522389100, 1522506558, 1520098043, 1519764349, 1521384566, 1520541898, 1521271618, 1520642366, 1520169730, 1521307430, 1520598946, 1520109560, 
1520416134, 1520639385, 1522308872, 1522167432, 1522609927, 1522327583, 1520186995, 1521399556, 1522283646, 1521580399, 1520947600, 1520421124, 1519712304, 1520583404, 1520349813, 1522426307, 1521776496, 1521143409, 
1521271618, 1520395530, 1519645678, 1522639423, 1521939121, 1520461036, 1522043512, 1522409242, 1520707332, 1521939139, 1520376024, 1521647387, 
1522804886, 1520700876), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
real_time = c("18:45:28", "23:57:42", "8:57:56", "13:42:43", 
"10:58:19", "12:52:56", "7:45:41", "12:7:19", "19:47:10", 
"21:16:28", "1:0:23", "17:59:4", "10:52:30", "8:32:11", "22:57:7", 
"16:50:28", "21:27:28", "11:3:12", "12:24:47", "12:57:33", 
"1:38:1", "0:10:10", "9:40:30", "6:38:47", "0:14:50", "19:11:16", 
"15:24:11", "21:45:21", "21:10:13", "15:41:11", "17:32:17", 
"17:41:36", "22:53:13", "18:34:29", "14:42:13", "23:13:1", 
"22:48:5", "12:57:38", "17:30:54", "23:30:7", "11:52:32", 
"9:13:52", "1:36:46", "22:23:53", "0:18:37", "15:12:5", "8:18:20", 
"12:17:53", "15:19:29", "6:33:32", "18:49:31", "12:46:26", 
"12:55:10", "7:23:36", "3:37:48", "1:28:54", "5:51:40", "14:29:18", 
"17:27:23", "20:45:49", "14:49:26", "20:44:58", "7:26:58", 
"0:39:26", "13:22:10", "17:23:50", "12:35:46", "20:39:20", 
"9:48:54", "23:49:45", "7:34:32", "16:17:12", "19:12:7", 
"12:46:23", "18:9:55", "18:59:16", "0:34:6", "21:13:19", 
"13:26:40", "11:12:4", "6:18:24", "8:16:44", "15:23:33", 
"16:11:47", "3:41:36", "19:50:9", "7:26:58", "4:5:30", "11:47:58", 
"3:23:43", "0:52:1", "22:17:16", "5:51:52", "11:27:22", "18:42:12", 
"0:52:19", "22:40:24", "15:49:47", "1:21:26", "16:54:36")), row.names = c(NA, -100L), class = c("tbl_df", "tbl", "data.frame"))

我想过滤以上数据并查找在两个小时之间发生的观察结果,例如09:30:0016:15:30。最好的方法是什么,也许使用lubridatehms软件包?

1 个答案:

答案 0 :(得分:0)

正如我在评论中所说,可以解决

start_data = "18:45:28"
end_date = "19:45:28"

# df - your data.frame
df[df$real_time > start_data  & df$real_time < end_date , ] 

输出:

      id           full_date real_time
26 12879 2018-03-29 19:11:16  19:11:16
51  3522 2018-03-06 18:49:31  18:49:31
73 14267 2018-04-01 19:12:07   19:12:7
75  2261 2018-03-04 18:09:55   18:9:55
76  8962 2018-03-18 18:59:16  18:59:16