Pergunta

I can't query xts for subsets including both a date and time range. For example:

> head(myxts['2012-01-06 9:30:00/2012-01-06 10:00:00'], 1)
                    Symbol       
2012-01-03 09:30:00 "AA"

> tail(myxts['2012-01-06 9:30:00/2012-01-06 10:00:00'], 1)
                Symbol       
2012-01-06 10:00:00 "AA"

Here the first row is 2012-01-03. Why not 2012-01-06?

EDIT

I've also tried to extract a single date first to a new variable, then query by time. In this case the date timeseries are extracted correctly but the time subset will not work.

e = myxts['2012-01-06']
e['10:00:00::10:20:00'] # returns all rows in '2012-01-06'

EDIT 2

Found this situation where entering '09:45:00' rather than '9:45:00' seems to fix the problem:

> tail(myxts['2011-12-19 9:40:00::2011-12-19 9:45:00'])
                    Symbol DaySec
2011-12-19 16:00:00 "WPI"  "57600"

> tail(myxts['2011-12-19 9:40:00::2011-12-19 09:45:00'])
                    Symbol DaySec  
2011-12-19 09:45:00 "WPI"  "35100"
Foi útil?

Solução

'2012-01-06 9:30:00' is not an ISO-8601 compliant string. Each time component must be two digits (if specified), so you need '2012-01-06 09:30:00'.

Outras dicas

Works for me:

R> now <- Sys.time()
R> foo <- xts(1:100, now + (1:100)*30*60)   # hundred half-hour intervals
R> head(foo)
                          [,1]
2012-02-21 11:48:32.37683    1
2012-02-21 12:18:32.37683    2
2012-02-21 12:48:32.37683    3
2012-02-21 13:18:32.37683    4
2012-02-21 13:48:32.37683    5
2012-02-21 14:18:32.37683    6
R> tail(foo)
                          [,1]
2012-02-23 10:48:32.37683   95
2012-02-23 11:18:32.37683   96
2012-02-23 11:48:32.37683   97
2012-02-23 12:18:32.37683   98
2012-02-23 12:48:32.37683   99
2012-02-23 13:18:32.37683  100

Now we can extract from the last hour on Feb 21 til the first on Feb 23:

R> xtract <- foo["2012-02-21 23:00::2012-02-23 01:00"]
R> head(xtract)
                          [,1]
2012-02-21 23:18:32.37683   24
2012-02-21 23:48:32.37683   25
2012-02-22 00:18:32.37683   26
2012-02-22 00:48:32.37683   27
2012-02-22 01:18:32.37683   28
2012-02-22 01:48:32.37683   29
R> tail(xtract)
                          [,1]
2012-02-22 22:18:32.37683   70
2012-02-22 22:48:32.37683   71
2012-02-22 23:18:32.37683   72
2012-02-22 23:48:32.37683   73
2012-02-23 00:18:32.37683   74
2012-02-23 00:48:32.37683   75
R> 

I can also extract within a day:

R> xtract <- foo["2012-02-23 02:00::2012-02-23 04:00"]
R> xtract
                          [,1]
2012-02-23 02:18:32.37683   78
2012-02-23 02:48:32.37683   79
2012-02-23 03:18:32.37683   80
2012-02-23 03:48:32.37683   81
R> 
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top