按动物园断点的日期过滤数据帧

时间:2016-04-11 20:54:53

标签: r datetime breakpoints zoo

我有以下CSV文件:

               f        , c
        1,19/11/2014 12:00,0.01
        2,19/11/2014 13:00,0.01
        3,20/11/2014 15:00,0.01
        4,20/11/2014 16:00,0.01
        5,20/11/2014 17:00,0.01  
        6,20/11/2014 19:00,0.01  
        7,20/11/2014 22:00,0.20  
        8,20/11/2014 23:00,0.03
        9,21/11/2014 16:00,0.01
        10,21/11/2014 17:00,0.01

我使用以下内容阅读了CSV文件:

library(strucchange)
ts1<-read.csv (file.choose())

我想过滤给定日期(DATE1和DATE2)之间的所有行。

DATE1 <- as.Date("20/11/2014 16:00", format = "%d/%m/%Y %H:%M")
DATE2 <- as.Date("20/11/2014 23:00", format = "%d/%m/%Y %H:%M")

因此,我将在数据框ts2中获得以下行:

           f        , c
    3,20/11/2014 15:00,0.01
    4,20/11/2014 16:00,0.01
    5,20/11/2014 17:00,0.01  
    6,20/11/2014 19:00,0.01  
    7,20/11/2014 22:00,0.20

对于ts1和ts2之间的转换,我尝试了以下方法:

    ts1$f<-as.Date(ts1$f, format = "%d/%m/%Y %H:%M")
    ts2<-ts1[ts1$f %in% DATE1:DATE2, ]
    ts2$f<-as.factor (ts2$f)

之后我将能够使用以下内容:

z<-read.zoo(ts2, tz = "", format = "%d/%m/%Y %H:%M", sep = ",")
bp <- breakpoints(z ~ 1, h = 2)

但是我收到以下错误:

>     bp <- breakpoints(z ~ 1, h = 2)
Error in `[[<-.data.frame`(`*tmp*`, i, value = c(1L, 11L, 6L, 3L, 4L,  : 
  replacement has 10 rows, data has 5

1 个答案:

答案 0 :(得分:1)

要记住R的一件事是Date类只适用于天而不是时间。因此,在这种情况下,strptime或as.POSIXct是使用的函数而不是as.Date。此外,您将日期/时间转换为POSIX对象,我建议不要转换回因素。

#Enter the data
ts1<-read.table(header = TRUE, sep=",", text="row, f        , c
        1,19/11/2014 12:00,0.01
                2,19/11/2014 13:00,0.01
                3,20/11/2014 15:00,0.01
                4,20/11/2014 16:00,0.01
                5,20/11/2014 17:00,0.01  
                6,20/11/2014 19:00,0.01  
                7,20/11/2014 22:00,0.20  
                8,20/11/2014 23:00,0.03
                9,21/11/2014 16:00,0.01
                10,21/11/2014 17:00,0.01 ")
#convert to date/time
ts1$f<-as.POSIXct(ts1$f, format="%d/%m/%Y %H:%M")
#create limits
DATE1 <- as.POSIXct("20/11/2014 16:00", format = "%d/%m/%Y %H:%M")
DATE2 <- as.POSIXct("20/11/2014 23:00", format = "%d/%m/%Y %H:%M")
#subset data from between limits
ts2<-subset(ts1, f>=DATE1 & f<=DATE2)