开发者

Looking ahead in a data set by n seconds

I have a set of data such as the following:

 TIME,VALUE
 09:00:00.0000000, 5.0  # observation 1
 09:00:00.0002326, 4.0  # observation 2
 ...
 09:00:30.0056464, 7.0  # observation n
 ...

I need to be able to look up the value 开发者_高级运维thirty out from the current observation and do a difference. For example, for observation 1 above, I'd grab observation n, and my difference would be 7.0 - 5.0 = 2.0. I want to do this for each observation in the original set.

The time values may not be exactly thirty seconds apart, so I would like to get the observation that is closest to the thirty seconds. It can be slightly greater than thirty seconds, but it should not be less.

Is there a design pattern for this in R, or a library that has code that would help me do this in the "R way" (vector-based)?


Use diff with the lag argument.

diff(your_data$TIME, lag = n)


In one line:

> which(x-time0>=30)[1]
[1] 10

Full explanation:

Create some sample data. time0 is the first element in a vector x of POSIXct dates.

> set.seed(1)
> options(digits.secs=3)
> basetime <- "2011-08-30 09:00:00"
> time0 <- as.POSIXct(strptime(basetime, "%Y-%m-%d %H:%M:%S"))
> x <- time0 + sort(runif(20, 0, 60))

Subtract time0 from each element of x. You can see that the 10th element happens to be the first time greater than 30s:

> x-time0
Time differences in secs
 [1]  3.707176 10.593405 12.100916 12.358474 15.930520 22.327434 22.802111
 [8] 23.046223 29.861954 34.371202 37.746843 39.647867 41.221371 43.057111
[15] 46.190485 46.646713 53.903381 54.492467 56.680516 59.514366

The following single line of code extracts this. Since the dates are sorted in ascending order, extract the first element of the subset with x-time0 > 30:

> which(x-time0>=30)[1]
[1] 10


Probably not the most efficent way but it will get the job done.

# Create a sequece of time values
fooDate<-seq(as.POSIXct("2011-01-01 00:00:00",tz="GMT"),as.POSIXct("2011-01-31 00:00:00",tz="GMT"),by="hours")
# And some fictional data
fooData<-rnorm(length(fooDate))
# Put it into a dataframe
foo <-data.frame(time=fooDate,data=fooData)
#Get the start time
exampleTime<-foo$time[1]
#A time 34 days in advance
desiredTime <- exampleTime+60*60*34
#Which row is it in.
index <- which(foo$time>=desiredTime)
#and to get it.
foo[index[1],2]-foo[1,2]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜