根据时间变量对数据帧进行子集化

Subset a dataframe based on time variable

我有以下数据框,它已经是一个更大数据框的子集:

                            Time X.N2O._ppm
    1    15/05/2015 13:30:07.291 0.03941801
    2    15/05/2015 13:30:08.307 0.01014003
    3    15/05/2015 13:30:09.323 0.02577801
    4    15/05/2015 13:30:10.338 0.02554231
    5    15/05/2015 13:30:11.354 0.02489800
    6    15/05/2015 13:30:12.370 0.02417584
    7    15/05/2015 13:30:13.386 0.02489115
    8    15/05/2015 13:30:14.402 0.02524912
    9    15/05/2015 13:30:15.417 0.02556182
    10   15/05/2015 13:30:16.433 0.02574274

我正在尝试根据时间变量进行子集化,但出现以下错误:

    subtime = subset(datasubcolrow, "Time" < 15/05/2015 13:30:15.417)
    Error: unexpected numeric constant in "subtime = subset(datasubcolrow, "Time" < 15/05/2015 13"

检查数据类型显示 Time 是数字:

    sapply(datasubcolrow, mode)
    Time X.N2O._ppm 
    "numeric"  "numeric" 

我需要将它转换成日期格式吗?我该怎么做?

谢谢

罗里

您可以将 'Time' 列更改为 'POSIXct' class 然后子集

datasubcolrow$Time <- as.POSIXct(datasubcolrow$Time, format='%d/%m/%Y %H:%M:%OS')

subset(datasubcolrow, Time < as.POSIXct('15/05/2015 13:30:15.417', 
              format='%d/%m/%Y %H:%M:%OS'))

数据

 datasubcolrow <- structure(list(Time = c("15/05/2015 13:30:07.291",
 "15/05/2015 13:30:08.307", 
 "15/05/2015 13:30:09.323", "15/05/2015 13:30:10.338", 
"15/05/2015 13:30:11.354", 
"15/05/2015 13:30:12.370", "15/05/2015 13:30:13.386", 
"15/05/2015 13:30:14.402", 
"15/05/2015 13:30:15.417", "15/05/2015 13:30:16.433"), 
X.N2O._ppm = c(0.03941801, 
0.01014003, 0.02577801, 0.02554231, 0.024898, 0.02417584, 0.02489115, 
0.02524912, 0.02556182, 0.02574274)), .Names = c("Time", "X.N2O._ppm"
), class = "data.frame", row.names = c("1", "2", "3", "4", "5", 
"6", "7", "8", "9", "10"))