How to define encounter periods by using first and last timestamps within a time series in R -
I work with electronic tagged fish. One snippet of my telemetry data (dataframe "D") is below each timestamp represents an identity for a unique fish. There are several different tags (personal fish). I want to classify detections for every fish during the encounter period by identifying a start time ("arrival") and an expiry time ("departure"), with an important value of 1 hour. For example, for the above fish (Tag ID 163), the output will be: I would like to create a loop (or any other code structure) which does the following: I do not know how to do it in the most effective way, and greatly appreciate your help. Thank you! You must first order your data according to the date. This is why you should convert your detection variable to a valid arbitrary type: POSIXct. Once you have ordered your data, My complete code here:
Tagged 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
for 1 in j: length (unique (d $ Tag ID)
diff , and
cumsum you can create a grouping variable to find a leap: here at least one hour (60 minutes) ) Has made a leap after. I am using
data.table in the Chinese syntax in Grouping Operations but this is not particularly important if you do not have a loose amount of data.
Library (data.table) ## Data Corsean D $ Detection & lt; - as.POSIXct (Striptime (D $ detection, '% m /% d /% Y% H:% M:% S')) ## Sort by using Tecetion d & lt; -d [Order (D $ detection), # ID variable is incremented, which is a one hour D $ id & lt; - Detects the jump of cumsum (C (F, round (D $ detection) / 60) & Gt; 60) ## You do not mention to select the place, Set DT (D) [, list (start = check [1], end = investigation [length (probe)], location = location [1], rivercom = rivercover [1], "tag id, id" # # tag id End of ID RiverCurm # 1: 163 0 2012-02-23 03:17:44 2012-02-23 03: 56: 25 Alcatraz_E 4.414 # 2: 163 1 2012-03-11 08:59:48 2012-03 -11 09:06:11 Alcatraz_N 4.414 # 3: 163 2 2012-04-14 15:10:20 2012- 04-14 15:12:11 Alcal Trajh_a 4.414
Comments
Post a Comment