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