0

I've got an xts object that contains data in a temporal resolution of seconds. For instance:

times <- seq.POSIXt(from=as.POSIXct("2023-04-01 00:00:00"), to=as.POSIXct("2023-04-04 00:01:00"), by = "sec")
xdata <- runif(n=length(times))
series <- xts(xdata, order.by=times)

What I want is a time series that uses every Xth element. For instance use only every 10th second. How do I accomplish this?

smoff
  • 592
  • 5
  • 21

1 Answers1

2

Note that the number of rows in series is 10 * 25926 + 1 = 259261 so if we start at the first row and take every 10th point we will start at the first point and end at the last point. We can do that using an integer, POSIXct or logical subscript vector as shown.

# 1
ix1 <- seq(1, nrow(series), 10) # 1 11 ... 259241 259251 259261
s1 <- series[ix1]

# 2
# "2023-04-01 00:00:00 EDT" "2023-04-01 00:00:10 EDT" ...  "2023-04-04 00:01:00 EDT"
tt <- seq(start(series), end(series), 10) # 
s2 <- series[tt]

# 3
ok <- .indexsec(series) %% 10 == 0 # T F F F F F F F F F T F F ...
s3 <- series[ok]

identical(s1, s2)
## [1] TRUE

identical(s1, s3)
## [1] TRUE

Another possibility is to start at 0 which effectively gives the 10th point as the first one and point 259260 as the last one if we use seq. If we use endpoints it always adds the last point so in that case 259261 will be the last point.

# 4
ix0 <- seq(0, nrow(series), 10) # 0 10 20 ... 259250 259260
s4 <- series[ix0]

# 5
ep <- endpoints(series, on = "seconds", k = 10) # 0 10 20 ... 259250 259260 259261
s5 <- series[ep]

identical(s5[-nrow(s5)], s4)
## [1] TRUE

Update

Expanded and rearranged answer.

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341