I work with electronically tagged fish. A snippet of my telemetry data (dataframe "d") is below. Each timestamp represents a detection for a unique fish.
TagID Detection Location RiverKm
163 02/23/2012 03:17:44 Alcatraz_E 4.414
163 02/23/2012 03:56:25 Alcatraz_E 4.414
163 04/14/2012 15:10:20 Alcatraz_E 4.414
163 04/14/2012 15:12:11 Alcatraz_N 4.414
163 03/11/2012 08:59:48 Alcatraz_N 4.414
163 03/11/2012 09:02:15 Alcatraz_N 4.414
163 03/11/2012 09:04:05 Alcatraz_N 4.414
163 03/11/2012 09:04:06 Alcatraz_N 4.414
163 03/11/2012 09:06:09 Alcatraz_N 4.414
163 03/11/2012 09:06:11 Alcatraz_E 4.414
There many different TagIDs (individual fish). I'd like to categorize the detections into encounter periods for each fish, by identifying a start time ("arrival") and an end time ("departure"), with a critical value of 1 hour. For example, for the above fish (TagID 163), the output would be:
TagID arrival departure Location RiverKm
163 02/23/2012 03:17:44 02/23/2012 03:56:25 Alcatraz_E 4.414
163 04/14/2012 15:10:2 04/14/2012 15:12:11 Alcatraz_N 4.414
163 03/11/2012 08:59:48 03/11/2012 09:06:11 Alcatraz_E 4.414
I'd like to create a loop (or any other code structure) that does the following:
for j in 1:length(unique(d$TagID))
- Identify the time of the first detection ("t1")
- IF the next detection for that tag in the time series ("t2") is less than one hour apart from t1, skip it and continue to the next detection; ELSE, place t1 in an "arrival" vector and t2 in a "departure vector.
- Stop when every arrival and departure timestamp has been categorized for each TagID.
I have no idea how to do this in the most efficient way, and would appreciate your help immensely.
Thank you!