1
votes

I am trying to subset intervals based on exact times from an irregular timeseries. The data is CO2 concentration measured every 3 Seconds. There are a few gaps whenever the instrument was connecting to the logger.

What I need to do, is substetting intervals like 00:00:24 to 00:00:57 and 00:01:24 to 00:01:57 for 24 Hours in a Month. I formatted my timeseries as POSIXct and tried on "zoo" and "xts" objects subsetting via window(x) to no avail. Another idea was creating a repeating flag or using a numeric index, but due to the connection to a central logging device I am missing a few time steps every 2 Hours.

My data looks like this:

12/1/2014 12:00:00 AM;   412.2;              
12/1/2014 12:00:03 AM;   412.1;              
12/1/2014 12:00:06 AM;   422.6;              
12/1/2014 12:00:09 AM;   427.7;              
12/1/2014 12:00:12 AM;   430.7;              
12/1/2014 12:00:15 AM;   430.5;              
12/1/2014 12:00:18 AM;   431.4;              
12/1/2014 12:00:21 AM;   432.9;              
12/1/2014 12:00:24 AM;   433.3;              
12/1/2014 12:00:27 AM;   433.6;              
12/1/2014 12:00:30 AM;   434.3;              
12/1/2014 12:00:33 AM;   434.4;              
12/1/2014 12:00:36 AM;   435.4;              
12/1/2014 12:00:39 AM;   434.3;             
12/1/2014 12:00:42 AM;   434.0;              
12/1/2014 12:00:45 AM;   434.1;              
12/1/2014 12:00:48 AM;   434.4;              
12/1/2014 12:00:51 AM;   434.5;              
12/1/2014 12:00:54 AM;   434.9;              
12/1/2014 12:00:57 AM;   434.7;             
12/1/2014 12:01:00 AM;   435.4;             
12/1/2014 12:01:03 AM;   435.0;             
12/1/2014 12:01:06 AM;   434.1;             
12/1/2014 12:01:09 AM;   432.4;             
12/1/2014 12:01:12 AM;   432.6;             
12/1/2014 12:01:15 AM;   433.3;             
12/1/2014 12:01:18 AM;   433.5;              

I am importing it as

library(zoo)
library(xts)
CO2 = read.csv(file="CO2_201412.csv", sep = ";", header = TRUE, stringsAsFactors = FALSE)

Reformat the index

CO2[,1]=as.POSIXct(CO2[,1], tz='GMT', format="%m/%d/%Y %I:%M:%S %p")

and convert it to a xts-object

CO2xts = xts(CO2[,2], CO2[,1])

or zoo-object

CO2zoo = zoo(CO2[,2], CO2[,1])

I would need something like this to work:

CO2win = window(CO2xts, start=as.POSIXct('%Y-%m-%d %H:%M:24'),end=as.POSIXct('%Y-%m-%d %H:%M:57'))

If I use fixed values like

CO2win = window(CO2xts, start=as.POSIXct('2014-12-01 12:00:24'),end=as.POSIXct('2014-12-01 12:00:57'))

It works like a charm, but as stated I need to query this time interval for every Minute, every Day in a Month.

As this is the first question I ask on SO, I am very thankful for any input on the formating of the question too.

1
Please provide a sample data file or code to reproduce it.zipp

1 Answers

2
votes

Like this?

sec <- as.integer(format(index(CO2xts),"%S"))
CO2xts[sec > 24 & sec < 57]
#                      [,1]
# 2014-12-01 00:00:27 433.6
# 2014-12-01 00:00:30 434.3
# 2014-12-01 00:00:33 434.4
# 2014-12-01 00:00:36 435.4
# 2014-12-01 00:00:39 434.3
# 2014-12-01 00:00:42 434.0
# 2014-12-01 00:00:45 434.1
# 2014-12-01 00:00:48 434.4
# 2014-12-01 00:00:51 434.5
# 2014-12-01 00:00:54 434.9