Search code examples
rtime-serieszoochronanytime

Selecting a time period with window in R; format "dd/mm/yyyy h:m:s" (Windows 7)


I have the following problem. I wanted to use the following code to select a specific time period for daily data, for instance this one:

window(Modellwind.zoo, start = as.Date("01/Jan/2001 12:00:00"), end = as.Date("4/Jan/2001 12:00:00"))

I get the following error message: Error in charToDate(x) : character string is not in a standard unambiguous format

When I tried to find an acceptable time format with the function anydate, it didnt work either:

library(anytime)
anydate("01/Jan/2001 12:00:00")

Using the following code:

window(Modellwind.zoo, start = as.Date("2001-01-01"), end = as.Date("2001-01-04")) 

Unfortunately, I'm getting the following error:

 **Warning messages:
  1: In which(in.index & all.indexes >= start & all.indexes <= end) :
  Incompatible methods ("Ops.dates", "Ops.Date") for ">="
  2: In which(in.index & all.indexes >= start & all.indexes <= end) :
  Incompatible methods ("Ops.dates", "Ops.Date") for "<="**

The following code includes a sample of Modellwind.zoo.

structure(c(9.08093655399134, 6.51590181162631, 7.14637946155745, 
1.43900253813098, 6.78880326680026, 14.3182887821646, 16.3360242476697, 
16.1781018622214, 17.2200845065928, 15.6439142273171, 8.10504553259712, 
3.78898221928137, 6.78608582121557, 7.18116948778303, 5.0299974451978, 
3.49148782050232, 6.9941692218925, 8.45512766287497, 12.0693672354131, 
11.987955907515, 10.3290912344961, 13.4506307038479, 21.7989491163794, 
14.0085737502259, 14.5883127217965, 11.8048508250059, 24.7915690531695, 
19.151192329502, 12.1739793389357, 11.9410486288817, 20.9967608089789, 
15.2111025271479, 5.90129944159158, 2.42733488656831, 7.20743282263504, 
22.737089035552, 14.8219437253637, 14.0558804343021, 8.98137356225915, 
12.9592918632241, 18.4870237580719, 9.11790624009415, 2.27721679625411, 
2.61125956054424, 3.26998407545227, 5.35392572192135, 4.95193258824599, 
6.86224460928498, 9.06594694653957, 12.4505570716657, 12.740653858499, 
15.8771799446521, 12.5618618366812, 3.58848453998801, 10.9966305297934, 
4.86674413518877, 10.7031531327265, 16.2043681264107, 12.0587344974091, 
2.10949588086561), index = structure(c(18659.5, 18660.5, 18661.5, 
18662.5, 18663.5, 18664.5, 18665.5, 18666.5, 18667.5, 18668.5, 
18669.5, 18670.5, 18671.5, 18672.5, 18673.5, 18674.5, 18675.5, 
18676.5, 18677.5, 18678.5, 18679.5, 18680.5, 18681.5, 18682.5, 
18683.5, 18684.5, 18685.5, 18686.5, 18687.5, 18688.5, 18689.5, 
18690.5, 18691.5, 18692.5, 18693.5, 18694.5, 18695.5, 18696.5, 
18697.5, 18698.5, 18699.5, 18700.5, 18701.5, 18702.5, 18703.5, 
18704.5, 18705.5, 18706.5, 18707.5, 18708.5, 18709.5, 18710.5, 
18711.5, 18712.5, 18713.5, 18714.5, 18715.5, 18716.5, 18717.5, 
18718.5), .Dim = 60L, format = structure(c("dd/mm/yyyy", "h:m:s"
), .Names = c("dates", "times")), origin = c(12L, 1L, 1949L), class = c("chron", 
"dates", "times")), class = "zoo")

Solution

  • The index of Modellwind.zoo is chron. That's why your attempts to subset by Date are not successful (your first attempt fails because as.Date expects a %Y-%m-%d format). Either change the index to Date before you subset by Date:

    Modellwind.zoo.Date <- Modellwind.zoo
    index(Modellwind.zoo.Date) <- as.Date(index(Modellwind.zoo.Date))
    window(Modellwind.zoo.Date, start = as.Date("2001-01-01"), end = as.Date("2001-01-04"))
    # 2001-01-01 2001-01-02 2001-01-03 2001-01-04 
    #   9.080937   6.515902   7.146379   1.43900
    

    Or leave the index as chron and subset by chron objects. Note that the origin of your chron index in Modellwind.zoo is 1949-12-01, not the default chron origin of 1990-01-01, so you need to specify it to avoid a warning about mis-matched origins.

    beg.chron <- as.chron("01/01/2001", origin = c(12, 1, 1949))
    end.chron <- as.chron("01/04/2001", origin = c(12, 1, 1949))
    window(Modellwind.zoo, start = beg.chron, end = end.chron)
    # (01/Jan/2001 12:00:00) (02/Jan/2001 12:00:00) (03/Jan/2001 12:00:00) 
    #               9.080937               6.515902               7.146379