I need to calculate the max value contained between the beginning of the day and the moment when the min value happened. This is a toy example of my dataset for one day and one dendro:
TIMESTAMP year DOY ring dendro diameter
1 2013-05-02 00:00:00 2013 122 1 1 3405
2 2013-05-02 00:15:00 2013 122 1 1 3317
3 2013-05-02 00:30:00 2013 122 1 1 3217
4 2013-05-02 00:45:00 2013 122 1 1 3026
5 2013-05-02 01:00:00 2013 122 1 1 4438
6 2013-05-03 00:00:00 2013 123 1 1 3444
7 2013-05-03 00:15:00 2013 123 1 1 3410
8 2013-05-03 00:30:30 2013 123 1 1 3168
9 2013-05-03 00:45:00 2013 123 1 1 3373
10 2013-05-02 00:00:00 2013 122 2 4 5590
11 2013-05-02 00:15:00 2013 122 2 4 5602
12 2013-05-02 00:30:00 2013 122 2 4 5515
13 2013-05-02 00:45:00 2013 122 2 4 4509
14 2013-05-02 01:00:00 2013 122 2 4 5566
15 2013-05-02 01:15:00 2013 122 2 4 6529
First, I calculated the MIN diameter for each day (DOY= day of the year) in each dendro (contained in one ring), also getting the time at what that min value happened:
library(plyr)
dailymin <- ddply(datamelt, .(year, DOY, ring, dendro),function(x)x[which.min(x$diameter), ])
Now, my problem is that I want to calculate the MAX diameter for each day. However, sometimes de max value occurs after the min value. I am only interested in the max value contained BEFORE the min value. I am not interested in the total max value if it happened after the min. Therefore, I need the max value contained (for each DAY) WITHIN THE TIME INTERVAL FROM THE BEGINNING OF THE DAY (00:00:00) TO THE THE MIN DIAMETER. Like I did with the min, I also need to know at what time that max value happened. This is what I want from the previous df:
year DOY ring dendro timeMin min timeMax max
1 2013 122 1 1 2013-05-02 00:45:00 3026 2013-05-02 00:00:00 3405
2 2013 123 1 1 2013-05-03 00:30:00 3168 2013-05-03 00:00:00 3444
3 2013 122 2 4 2013-05-02 00:45:00 4509 2013-05-02 00:00:15 5602
As you can see, the min value is the actual min value. However, the max value I want is not the max value of the day, it is the max value that happened between the beginning of the day and the min value. My first attempt, unsuccessful, returns the max value of the day, even in it is out of the desired time interval:
dailymax <- ddply(datamelt, .(year, DOY, ring, dendro),
function(x)x[which.max(x$diameter[1:which.min(datamelt$diameter)]), ])
Any ideas?