I have two data frames, the first of which spans 3 months and has recordings of depth every 2.5 minutes.
shark depth temperature datetime date location
A 49.5 26.2 20/03/2018 08:00 20/03/2018 SS04
A 49.5 25.3 20/03/2018 08:02 20/03/2018 SS04
A 53.0 24.2 20/03/2018 08:04 20/03/2018 SS04
A 39.5 26.5 20/03/2018 08:32 20/03/2018 Absent
A 43.0 26.2 21/03/2018 09:10 21/03/2018 Absent
A 44.5 26.5 21/03/2018 10:18 21/03/2018 SS04
I have a second data frame which lists the tidal state during these three months.
date time depth tide_state datetime
18/03/2018 02:33 2.09 High 20/03/2018 02:33
18/03/2018 08:39 0.45 Low 20/03/2018 08:39
18/03/2018 14:47 2.14 High 20/03/2018 14:47
18/03/2018 20:54 0.41 Low 20/03/2018 20:54
19/03/2018 03:01 2.13 High 21/03/2019 03:01
19/03/2018 09:09 0.41 Low 21/03/2019 09:09
I would like to create a new dataset which inserts the tidal state for all the values on the first dataset, based on the datetime column in each. For example if low tide was at 08:39, and high tide at 14:47, I would like every value in df1 greater than 08:39 but less than 14:47 to be recorded as 'Low', and values after this but before the next low tide to be 'High'.
As the time of the tides change three to four times per day, I'm not quite sure how I could go about merging these in R. I'm not sure if there is an easy way to do this using data.table?
I have both datetime columns in each dataframe set as POSIXct values.
Ideally I would like to produce a table a dataframe like this:
shark depth temperature datetime date location tide_state
A 49.5 26.2 20/03/2018 08:00 20/03/2018 SS04 High
A 49.5 25.3 20/03/2018 08:02 20/03/2018 SS04 High
A 53.0 24.2 20/03/2018 08:04 20/03/2018 SS04 High
A 39.5 26.5 20/03/2018 08:32 20/03/2018 Absent Low
A 43.0 26.2 20/03/2018 09:10 21/03/2018 Absent Low
A 44.5 26.5 20/03/2018 10:18 21/03/2018 SS04 Low