0

I have a zoo object that contains velocity data from two different points (V1 and V2), as well as particle Data from the same two points. The distance between the two points is 170m.

Date<- as.POSIXct("2012-01-01 08:00:00") + 1:120
V1<-rnorm(200,mean=5) #Velocity in m/sec
R<-rnorm(4,mean=3)
V2<-V1+R #Velocity in m/sec
Data1<-rnorm(200, mean=20)
Data2<-rnorm(200, mean=25)
V<-data.frame(V1,V2,Data1,Data2)
z<-zoo(as.matrix(V),order.by=Date)
L<-170 #Length =170m

If I average the velocity data

z$Avg_Vel<-rowMeans(z[,1:2])

I should have a pretty good idea of how fast the particles are traveling, and since I know the distance I should have a good idea of how long it is taking the particles to travel from Point 1 to Point 2 during the course of the time series.

z$Off<-L/z$Avg_Vel

But I cant figure out how to offset my zoo object to account for the time delay it takes for particles to travel between the two points. So if I am interested in finding the difference between Data 1 and Data 2, I don't want to do

Diff<-z$Data1-z$Data2

As this does not include the offset

If it takes 2 minutes for the particles to travel from point 1 to point 2, than I would want

Diff<-z$Data1-z$Data2(+2min)

So that I am looking at the difference between Data1 at time x, and Data2 at time x+2min

To clarify in response to an answer, the end result would be a rolling offset. So that

  Offset<-z$Off

Looking at this kind of Offset

round(as.numeric(z$Off))

The result would look like this

1 Diff<- Diff<-z$Data1-z$Data2(+22 sec)
2 Diff<- Diff<-z$Data1-z$Data2(+23 sec)
3 Diff<- Diff<-z$Data1-z$Data2(+32 sec)..........
Vinterwoo
  • 3,843
  • 6
  • 36
  • 55
  • I should mention that I have been able to offset my time by applying the offset to one of the data frames before I merge them. But I was hoping to be able to offset my time within a single zoo object – Vinterwoo Sep 29 '12 at 19:57
  • Can you explain what zoo function does in line #8? – Sathish Oct 06 '12 at 06:12

1 Answers1

1

This is a way to include an offset:

offset <- 120 # 2 minutes in seconds

ix <- index(z) + offset # new time index

Calculate the difference with a 2-minute offset:

z$Data1[rev(index(z) %in% ix)] -
 as.numeric(z$Data2[index(z) %in% ix])

Your example time series is too short for an offset of 2 minutes. I tested it with a 1-minute offset instead (offset = 60).


If you want to use a vector of offsets, use this:

offsets <- sample(1:5, nrow(z), TRUE) # some example offsets (in ms)
# alternatively you could use:
# round(as.numeric(z$Off))

ixs <- index(z) + offsets

ixs_num <- match(ixs, index(z), nomatch = NA)

z$Data1[seq(length(ixs_num))[!is.na(ixs_num)]] -
 as.numeric(z$Data2)[na.omit(ixs_num)]

Note. This procedure works for both positive and negative offsets.

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
  • Thanks for that, it works quite well for a 1 min offset. I should have been more explicit as I was using the "2 min" offset as an example. What I was hoping for was a rolling offset based on the velocity. So in this case for your above code offset<-z$Off – Vinterwoo Oct 05 '12 at 11:21
  • @Vinterwoo I extended my answer by a way to use a vector of offsets. – Sven Hohenstein Oct 05 '12 at 12:19