-1

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!

Herr Student
  • 853
  • 14
  • 26
  • 2
    I didn't downvote but can understand why the downvote occurred. Right now your question is not clear. What does `subset seconds` mean? You have multiple questions rather than a well framed **How do I...** question. Perhaps a better approach is to say this is the data I have (give an example) and this is the way I'd like the data to look (give an example). At the very least tighten up the question to express what you're after. – Tyler Rinker May 28 '13 at 15:46
  • Now your question says you want to extract the row that does not end in 00 which is contradictory to your previous request to "make time series regular". Which do you want? – GSee May 28 '13 at 16:18
  • I want to see what happened at the rows not ending with 00, but want to extract all having 00 later. – Herr Student May 28 '13 at 16:59

1 Answers1

1

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
GSee
  • 48,880
  • 13
  • 125
  • 145