R Code for Extracting Independent Events

189 views
Skip to first unread message

Nia Smith

unread,
Aug 5, 2021, 9:33:50 AM8/5/21
to camtrapR
Dear camtrapR Community,

I hope you are all doing well?

I apologize for asking a question slightly unrelated to the group... I don't have anywhere else to ask.. I have a camera trapping data set which I would like to extract only the independent events from. With independence taken at 30 minutes. Could anyone please be so kind to help me find such code? My data is not in the normal camtrapR format, and basically just in a normal R dataframe with the following columns: Station, Date, Time, Species, and DateTimeOriginal.

The code below is the closest I can get, but it's not quite right... It deletes more records than it should...
#####################################################
# Fake data:
df <- structure(
  list(
    spp = c("X", "X", "X", "X", "X", "X", "X", "X", "X", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Z", "Z", "Z", "Z", "Z", "Z", "Z"),
    loc = c(1, 1, 1, 1, 1, 1, 2, 3, 3, 1, 1, 2, 3, 3, 3, 4, 2, 3, 3, 3, 3, 3, 4),
    date_time = structure(c(1609451755, 1609452355, 1609581600, 1609582202, 1609583403, 1609585204, 1609668005, 1609538155, 1609841700, 1609495204, 1609583406, 1609668000, 1609539071, 1609756205, 1609758001, 1609581600, 1609668067, 1609451793, 1609452213, 1609452633, 1609495200, 1609626404, 1609581600), tzone = "", class = c("POSIXct", "POSIXt")),
    date = structure(c(18627, 18628, 18629, 18629, 18629, 18629, 18630, 18628, 18632, 18628, 18629, 18630, 18629, 18631, 18631, 18629, 18630, 18627, 18628, 18628, 18628, 18630, 18629), class = "Date"),
    time = c("23:55:55", "00:05:55", "12:00:00", "12:10:02", "12:30:03", "13:00:04", "12:00:05", "23:55:55", "12:15:00", "12:00:04", "12:30:06", "12:00:00", "00:11:11", "12:30:05", "13:00:01", "12:00:00", "12:01:07", "23:56:33", "00:03:33", "00:10:33", "12:00:00", "00:26:44", "12:00:00")),
  row.names = c(NA, -23L), class = "data.frame")
#####################################################
# Bad code
df %>%
  arrange(date_time) %>%
  group_by(spp, loc) %>%
  filter(
    as.integer(
      difftime(date_time, lag(date_time), units = 'secs')
    ) > 1800 | row_number() == 1) %>%
  ungroup()
#####################################################

Any guidance would be well appreciated! Thanks!

:)

Best Wishes,
Nia
Message has been deleted

Juergen Niedballa

unread,
Aug 10, 2021, 11:21:03 AM8/10/21
to camtrapR
Hi Nia, 
search in the forum for the keyword "assessTemporalIndependence". It is an internal function of camtrapR that can be used for filtering a record table by time differences. It's not a pretty solution and doesn't do any of the checks that camtrapR normally does, but it seems to work. 

Based on your code, here's an example. 

colnames(df) <- c("Species", "Station", "DateTimeOriginal", "Date", "Time")
df$Station <- as.character(df$Station)


df_filt <- camtrapR:::assessTemporalIndependence(df, 
                                      deltaTimeComparedTo = "lastIndependentRecord",
                                      stationCol = "Station",
                                      camerasIndependent = FALSE,
                                      columnOfInterest = "Species",
                                      minDeltaTime = 30)    # minutes


You may also want to look at the difference between deltaTimeComparedTo = "lastIndependentRecord" and "lastRecord". For more details on that, see ?recordTable


Please don't use any assessTemporalIndependence scripts files I may have attached to previous threads. Better to install the latest version of camtrapR and then use the internal function with camtrapR:::assessTemporalIndependence()

Is the output what you'd expect? Seemed ok at first glance, but please check thoroughly. 

Best regards,
Jürgen

Reply all
Reply to author
Forward
0 new messages