I'm trying to find the first time 'price' is higher than another value called 'dayhigh' per day.
I've had issues with getting this result into a time series object so I'm just using the POSIXlt class for datetime and the day for reference is in Date class. Example data is in a frame called 'example':
day,datetime,price,dayhigh
2016-09-01,2016-09-01 15:00:00,1.11912,1.11990
2016-09-01,2016-09-01 15:00:00,1.13000,1.11990
2016-09-01,2016-09-01 15:00:01,1.11911,1.11990
2016-09-05,2016-09-05 15:00:00,1.11436,1.11823
2016-09-05,2016-09-05 15:00:01,1.11436,1.11823
2016-09-05,2016-09-05 15:00:01,1.11900,1.11823
2016-09-05,2016-09-05 15:00:01,1.11436,1.11823
2016-09-06,2016-09-06 15:00:00,1.12383,1.12557
2016-09-06,2016-09-06 15:00:00,1.12382,1.12557
2016-09-06,2016-09-06 15:00:00,1.12382,1.12557
2016-09-06,2016-09-06 15:00:00,1.12384,1.12557
2016-09-06,2016-09-06 15:00:00,1.12384,1.12557
2016-09-06,2016-09-06 15:00:00,1.12558,1.12557
2016-09-06,2016-09-06 15:00:01,1.12559,1.12557
df = data.frame(
day = c("2016-09-01", "2016-09-01", "2016-09-01", "2016-09-05", "2016-09-05",
"2016-09-05", "2016-09-05", "2016-09-06", "2016-09-06", "2016-09-06",
"2016-09-06", "2016-09-06", "2016-09-06", "2016-09-06"),
datetime = c("2016-09-01 15:00:00", "2016-09-01 15:00:00", "2016-09-01 15:00:01",
"2016-09-05 15:00:00", "2016-09-05 15:00:01", "2016-09-05 15:00:01",
"2016-09-05 15:00:01", "2016-09-06 15:00:00", "2016-09-06 15:00:00",
"2016-09-06 15:00:00", "2016-09-06 15:00:00", "2016-09-06 15:00:00",
"2016-09-06 15:00:00", "2016-09-06 15:00:01"),
price = c(1.11912, 1.13, 1.11911, 1.11436, 1.11436, 1.119, 1.11436,
1.12383, 1.12382, 1.12382, 1.12384, 1.12384, 1.12558, 1.12559),
dayhigh = c(1.1199, 1.1199, 1.1199, 1.11823, 1.11823, 1.11823, 1.11823,
1.12557, 1.12557, 1.12557, 1.12557, 1.12557, 1.12557, 1.12557)
)
One idea I've had is to split the frame by day into a list of frames:
exlist <- split(example, as.Date(example$day))
This returns a list of objects.
What I'd like to do is to use which.max
on each frame object within the list and add a 'TRUE' into a new column in each frame for the row where the day's first high happens. The day's first high defined as the first price > dayhigh
for each day.
From there I can concatenate back into a single frame and perform further analysis.