1

I am currently using split.xts in the form of split(xts.obj,'days') to create lists of intra-day data, so that each element of the list represents one day. This breaks up a day from midnight to (just before) midnight the next day is there a way to split up the data into the lists at an arbitrary time, e.g. say 9am till 08:59.59.999am the next day?

It's a reasonably generic question but if you want sample data...here it is...The below should illustrate my point about the split occurring at midnight.

require(xts)
x <- xts(rnorm(1000000),Sys.time()-1000000:1)
x1 <- split(x,'days')
head(x1[[2]])

EDIT:

The solution is very similar to one produced as the answer to this question... How do I extract / subset day+0 to day+1 index times from minutely data via xts in R?, but if there is a more direct way of doing it that would be appreciated...

Community
  • 1
  • 1
h.l.m
  • 13,015
  • 22
  • 82
  • 169

2 Answers2

2

This creates a vector of times (for GMT 9AM):

as.POSIXct(as.Date( seq(range(index(x))[1], range(index(x))[2], by="days") )) + 60*60*9
cts <- .Last.value
xp9 <- split(x, cut(index(x), cts) )
str(xp9)
#List of 11
IRTFM
  • 258,963
  • 21
  • 364
  • 487
0

This is the same as your other question (?), with the same possible solution:

library(xts)
set.seed(42)
x <- xts(rnorm(1000000), as.POSIXct('2012-10-07') - (1000000:1) )

index(x) = index(x) - (9*3600)
x1 <- lapply ( split(x,'days'), function(one_day){ index(one_day) = index(one_day) + 9*3600; one_day } )
index(x) = index(x) + (9*3600)

Here is x:

2012-09-25 10:13:20  1.3709584
2012-09-25 10:13:21 -0.5646982
2012-09-25 10:13:22  0.3631284
...
2012-10-06 23:59:57  0.7505021
2012-10-06 23:59:58 -0.4726833
2012-10-06 23:59:59  1.1356617

Here is x1[1]:

2012-09-25 10:13:20  1.3709584
2012-09-25 10:13:21 -0.5646982
2012-09-25 10:13:22  0.3631284
...
2012-09-26 08:59:57 -0.7079315
2012-09-26 08:59:58 -0.2135840
2012-09-26 08:59:59 -1.8307128

x1[[2]]:

2012-09-26 09:00:00  2.3205603
2012-09-26 09:00:01  1.8911404
2012-09-26 09:00:02 -0.8547244
...
2012-09-27 08:59:57 -0.5731661
2012-09-27 08:59:58 -1.5224021
2012-09-27 08:59:59 -0.5316183

And x1[[12]]:

2012-10-06 09:00:03  0.9222899
2012-10-06 09:00:04 -0.2010127
2012-10-06 09:00:05 -1.8403161
...
2012-10-06 23:59:54 -0.5931701
2012-10-06 23:59:55 -1.1656284
2012-10-06 23:59:56  0.7000441

If you want an alternative approach, you could also just change the timezone:

Sys.setenv(TZ = "UTC")
library(xts)
set.seed(42)
x <- xts(rnorm(1000000), as.POSIXct('2012-10-07') - (1000000:1) )
indexTZ(x) = 'UTC+9'
x1 <- lapply ( split(x,'days'), function(one_day){ indexTZ(one_day) = 'UTC'; one_day } )

If your 9am day break is actually 8am in summertime, then you should use the timezone approach, as R will do the summertime adjustments automatically for you. If not, I prefer the previous approach: I'm then describing exactly what I mean, and not vulnerable to a bug in the timezone database. (I think the TZ database might also work differently on Windows... I've not had chance to test that yet.)

Community
  • 1
  • 1
Darren Cook
  • 27,837
  • 13
  • 117
  • 217