在13:30:00和13:32:00之间设置子数据在Mac上有效,但在Windows上无效,为什么?

时间:2019-02-18 10:47:15

标签: r date time subset

我有一些二氧化碳浓度的时间序列。我正在尝试提取13:30:00和13:32:00之间的数据,但是在Windows上无法正常工作。

 head(df)
# A tibble: 6 x 6
# Groups: Sensor [1]
  Sensor Date       Time   CO2.ppm. Type   calCO2
  <fctr> <date>     <time>    <dbl> <chr>   <dbl>
1 N1     2019-02-12 13:30       583 Naveed    400
2 N1     2019-02-12 13:30       583 Naveed    400
3 N1     2019-02-12 13:30       583 Naveed    400
4 N1     2019-02-12 13:31       583 Naveed    400
5 N1     2019-02-12 13:32       583 Naveed    400
6 N1     2019-02-12 13:33       583 Naveed    400

这在Mac上有效,但在Windows上不行,有什么想法吗?:

require(lubridate)
#require(tidyverse) 

subset(df,Time>=hms("13:30:00") & Time < hms("13:32:00"))

1 个答案:

答案 0 :(得分:1)

您可以使用as.POSIXct()创建日期/时间列和子集。

d$Date.px <- as.POSIXct(paste(d$Date, d$Time))
subset <- d[d$Date.px < as.POSIXct("2019-02-12 13:32"), ]
# > subset
#   Sensor       Date  Time CO2.ppm.   Type calCO2             Date.px
# 2     N1 2019-02-12 13:30      583 Naveed    400 2019-02-12 13:30:00
# 3     N1 2019-02-12 13:30      583 Naveed    400 2019-02-12 13:30:00
# 4     N1 2019-02-12 13:30      583 Naveed    400 2019-02-12 13:30:00
# 5     N1 2019-02-12 13:31      583 Naveed    400 2019-02-12 13:31:00

数据

d <- structure(list(Sensor = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("<fctr>", 
"N1"), class = "factor"), Date = structure(c(2L, 2L, 2L, 2L, 
2L, 2L), .Label = c("<date>", "2019-02-12"), class = "factor"), 
    Time = structure(c(2L, 2L, 2L, 3L, 4L, 5L), .Label = c("<time>", 
    "13:30", "13:31", "13:32", "13:33"), class = "factor"), CO2.ppm. = structure(c(2L, 
    2L, 2L, 2L, 2L, 2L), .Label = c("<dbl>", "583"), class = "factor"), 
    Type = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("<chr>", 
    "Naveed"), class = "factor"), calCO2 = structure(c(2L, 2L, 
    2L, 2L, 2L, 2L), .Label = c("<dbl>", "400"), class = "factor"), 
    Date.px = structure(c(1549974600, 1549974600, 1549974600, 
    1549974660, 1549974720, 1549974780), class = c("POSIXct", 
    "POSIXt"), tzone = "")), row.names = 2:7, class = "data.frame")