Following is my data frame lanec
:
read.table(textConnection(scan(,character(),sep="\n")))
vehicle.id frame.id svel PrecVehVel
1 2 1 55 59
2 2 2 55 59
3 2 3 53 57
4 2 4 50 54
5 2 5 48 52
6 3 3 49 53
7 3 4 55 59
8 3 5 55 59
9 3 6 43 47
10 3 7 45 49
11 3 8 52 56
12 3 9 50 54
13 4 1 38 42
14 4 2 42 46
15 4 3 45 49
16 4 4 48 52
17 4 5 50 54
18 4 6 52 56
19 4 7 55 59
20 5 6 49 53
21 5 7 52 56
22 5 8 54 58
23 5 9 58 62
24 5 10 60 64
25 5 11 63 67
26 5 12 70 74
<Carriage return>
I want to find correlation cor
between svel
and PrecVehVel
(vehicle's velocity and preceding vehicle's velocity respectively) by vehicle.id
for every 3 rows but for consecutive rows. This means that in the data frame lanec
for vehicle.id==2
, R should first find correlation between
svel PrecVehVel
1 55 59
2 55 59
3 53 57
svel(55,55,53) and PrecVehVel(59,59,57), then start again from the second row and find correlation between
svel PrecVehVel
2 55 59
3 53 57
4 50 54
svel(55,53,50) & PrecVehVel(59,57,54) and so on.
The output should be something like this:
vehicle.id frames speed.cor
2 1 - 3 1
2 2 - 4 1
2 3 - 5 1
2 4 - 5 1
Note that the last entry in frames
column has only 2 frames for which the correlation was found because there was no more data for vehicle 2.
The best I could do with my limited knowledge of R was following:
ddply(lanec, 'vehicle.id', summarize, speed.cor = cor(svel, PrecVehVel) )
But this clearly doesn't meet the goal because it finds the correlation for all the rows for a vehicle.id