Here is the data:
# vector1 dates
reading.dates <- as.Date(c("2012-02-13","2012-02-20","2012-02-28"))
mydat <- data.frame (ID = c("A", "B", "C", "D"), Date1 = c(1:4),
Date2 = c(5:8), Date3 = c(11:14))
mydat
ID Date1 Date2 Date3
1 A 1 5 11
2 B 2 6 12
3 C 3 7 13
4 D 4 8 14
Here is what I want to do:
for each ID levels,
Between date1 and date2
calculate difference is dates –
reading.dates[2] - reading.dates[1] = 7 = say "Y"
Add correponding date values and average –
(date1 + date2) / 2 = say "X"
then multiply = X * Y
for indiviudual A:((1 + 5) / 2)* 7 )
Between date2 and date3
Similarly between date2 and date3 ….so on to end of the file, infact I have more variables
reading.dates[3] – reading.dates[2]
Time difference of 8 days
for A indvidual (5+11)/2) * 8
then add the all values.
for A it would be
((1 + 5) / 2)* 7 ) + ((5+11)/2) * 8)
Thanks
Try this code, I think it should work for any size of data.frame and date vector:
You can change yourself how you want to have the data returned.