简体   繁体   English

根据时间变量对数据框进行分组

[英]Subset a dataframe based on time variable

I have the following dataframe which is already a subset of a much larger dataframe: 我有以下数据框,它已经是更大数据框的子集:

                            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

I'm trying to subset based on the Time variable but get the following error: 我正在尝试基于Time变量进行子集化,但出现以下错误:

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

Checking data types shows that Time is numeric: 检查数据类型显示时间是数字:

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

Do I need to convert it into a date format and how to I go about doing this? 我是否需要将其转换为日期格式,该如何进行?

Thanks 谢谢

Rory 罗里

You can change the 'Time' column to 'POSIXct' class and then subset 您可以将“时间”列更改为“ 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'))

data 数据

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM