Question

Is there a possiblity to subset seconds in xts?

2013-01-01 00:01:00 2.2560000
2013-01-01 00:02:00 2.3883333
2013-01-01 00:03:00 1.8450000
2013-01-01 00:04:00 1.6966667
2013-01-01 00:04:03 1.3100000
2013-01-01 00:05:00 0.8533333

I want to get the line not having :00 seconds in the End!

2013-01-01 00:04:03 1.3100000

Usually I would subset by Time [T09:00/T09:30] but right now I want the lines not having a timestamp with 00 for seconds. Thanks!

Was it helpful?

Solution

First, I have to get your example data into R (please use dput next time)

lines <- '2013-01-01 00:01:00 2.2560000
2013-01-01 00:02:00 2.3883333
2013-01-01 00:03:00 1.8450000
2013-01-01 00:04:00 1.6966667
2013-01-01 00:04:03 1.3100000
2013-01-01 00:05:00 0.8533333'

tmp <- read.table(text=lines)
x <- xts(tmp[, 3], as.POSIXct(paste(tmp[, 1], tmp[, 2])))

You can use the .indexsec function to extract rows where the second is (or isn't) 0.

x[.indexsec(x) == 0]
#                         [,1]
#2013-01-01 00:01:00 2.2560000
#2013-01-01 00:02:00 2.3883333
#2013-01-01 00:03:00 1.8450000
#2013-01-01 00:04:00 1.6966667
#2013-01-01 00:05:00 0.8533333

x[.indexsec(x) != 0]
#                    [,1]
#2013-01-01 00:04:03 1.31

Another idea would be to use the unexported xts:::startof function which is analogous to the endpoints function.

x[xts:::startof(x, "mins")]
#                         [,1]
#2013-01-01 00:01:00 2.2560000
#2013-01-01 00:02:00 2.3883333
#2013-01-01 00:03:00 1.8450000
#2013-01-01 00:04:00 1.6966667
#2013-01-01 00:05:00 0.8533333

Or, if you only one the row that does not end in 00, you can use negative subsetting:

x[-xts:::startof(x, "mins")]
#                    [,1]
#2013-01-01 00:04:03 1.31

Here's how to do it by merging with a zero width xts object that has the index that you want.

merge(xts(, seq(start(x), end(x), by="min")), x, all=FALSE)
#                            x
#2013-01-01 00:01:00 2.2560000
#2013-01-01 00:02:00 2.3883333
#2013-01-01 00:03:00 1.8450000
#2013-01-01 00:04:00 1.6966667
#2013-01-01 00:05:00 0.8533333
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top