使用日期/时间因子变量子集数据框

时间:2017-10-23 21:40:57

标签: r datetime subset

我希望这是一个重复的问题,但我现在花了很多时间试图找到解决方案,并且非常感谢一些帮助。

我在数据框中有一个变量timestamp,当前存储为一个因子。 timestamp是日期和时间,格式为dd / mm / yyyy hh:mm:ss:ssssss

我希望能够使用timestamp变量对数据帧进行子集化,例如09/10/2017 00:02:00和09/10/2017 00:06:00之间的所有行。

我已尝试转换为有序因子,并尝试转换为POSIXlt来帮助进行子集化,但没有成功。

提前感谢您的任何帮助

df <- data.frame(timestamp=c("09/10/2017 00:00:00:000000", "09/10/2017 00:01:00:000000", "09/10/2017 00:02:00:000000", 
                 "09/10/2017 00:03:00:000000", "09/10/2017 00:04:00:000000", "09/10/2017 00:05:00:000000",
                 "09/10/2017 00:06:00:000000", "09/10/2017 00:07:00:000000", "09/10/2017 00:08:00:000000", 
                 "09/10/2017 00:09:00:000000", "09/10/2017 00:10:00:000000", "09/10/2017 00:00:00:000000", 
                 "09/10/2017 00:01:00:000000", "09/10/2017 00:02:00:000000", "09/10/2017 00:03:00:000000", 
                 "09/10/2017 00:04:00:000000", "09/10/2017 00:05:00:000000", "09/10/2017 00:06:00:000000", 
                 "09/10/2017 00:07:00:000000", "09/10/2017 00:08:00:000000", "09/10/2017 00:09:00:000000", 
                 "09/10/2017 00:10:00:000000"), b=c (1:22))

1 个答案:

答案 0 :(得分:1)

以下是使用lubridate

的解决方案
require(lubridate);

# Convert timestamps to POSIXct time&date
df$timestamp <- dmy_hms(gsub(":000000", "", df$timestamp));

# These are your query start/stop dates&times
start <- "09/10/2017 00:02:00";
stop <- "09/10/2017 00:06:00";
interval <- interval(dmy_hms(start), dmy_hms(stop));

# Return entries that fall within query interval
df[df$timestamp %within% interval, ];
#        timestamp  b
#3  2017-10-09 00:02:00  3
#4  2017-10-09 00:03:00  4
#5  2017-10-09 00:04:00  5
#6  2017-10-09 00:05:00  6
#7  2017-10-09 00:06:00  7
#14 2017-10-09 00:02:00 14
#15 2017-10-09 00:03:00 15
#16 2017-10-09 00:04:00 16
#17 2017-10-09 00:05:00 17
#18 2017-10-09 00:06:00 18

或使用subset(df, timestamp %within% interval)给出相同的结果。最好将其包装在一个更常用的功能中。