1

I would to subset the xts object(data) by selecting only the data that is within the range of 09:00:00.000 to 17:00:00.000 across all the different dates in the file.

Timestamp                Col1  Col2  Col3  Col4 
2017-05-02 08:50:47.457 12345 12345 12345 12345 
2017-05-02 09:50:49.845 12345 12345 12345 12345
2017-05-02 16:50:49.845 12345 12345 12345 12345
2017-05-02 22:50:50.085 12345 12345 12345 12345
2017-05-03 08:50:47.457 12345 12345 12345 12345 
2017-05-04 09:50:49.845 12345 12345 12345 12345
2017-05-04 16:50:49.845 12345 12345 12345 12345
2017-05-04 22:50:50.085 12345 12345 12345 12345

How can I do this ?

mynameisJEFF
  • 4,073
  • 9
  • 50
  • 96
  • 1
    Search existing answers, there are tons of existing duplicates. – smci Jun 10 '17 at 05:47
  • I am asking how to subset a time range across ALL dates not the normal case where`xts_obj["2017-05-02 09:00:00.000::2017-05-02 17:00:00.000"]`, because I do know how to specify a range for a specific date. I am talking about across different dates. – mynameisJEFF Jun 10 '17 at 05:51
  • Extract the time field, then filter on that. It's still a duplicate, like I'm telling you. You can either use a compound comparison with logical indexing `[time>= tmin & time <= tmax]` or data.table or dplyr's `between()`. There are tons of existing duplicates. Please find the best one then close-as-duplicate this question into it. – smci Jun 10 '17 at 05:55

1 Answers1

1

Since you are asking for an xts solution, it is pretty straightforward. Just subset by using the brackets.

t<-read.table("test.csv", sep=";", header = T)
library(xts)
t2<-xts(t[,2:5], order.by = as.POSIXct(t$Timestamp, tz="UTC","%Y-%m-%d %H:%M:%S"))
t2["T09:00/T17:00"]

Alternative version using the nice builtin index functions (this can be used for more complicated operations):

 t2[.indexhour(t2) %in% seq(9,17)]
Buggy
  • 2,050
  • 21
  • 27