r语言 - 基于时间变量的数据帧子集



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

                            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" 

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

感谢

Rory

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

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

相关内容

  • 没有找到相关文章

最新更新