根据时间变量子集数据帧

时间:2015-05-21 09:55:18

标签: r date datetime time subset

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

                            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"

检查数据类型显示时间是数字:

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

我是否需要将其转换为日期格式以及如何进行此操作?

由于

1 个答案:

答案 0 :(得分:0)

您可以更改“时间”&#39;列到&#39; POSIXct&#39;类,然后子集

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"))