I am trying to create a new column that assigns a unique value to the observation (row) only IF the recorded observation occur after a specific time following the last observation (see data frame).
Context:
I set up camera trap to observe what species visit a particular plot, every visit by a species
should get a unique visitID
. The actual database contains more complexity but this is the main problem I have.
new.df <- data.frame(
species = c("A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B"),
visit.time = c(seq(ymd_hm('2015-01-01 00:00'), ymd_hm('2015-01-01 00:10'), by = '2 mins'),
seq(ymd_hm('2015-01-01 00:00'), ymd_hm('2015-01-01 00:10'), by = '2 mins'))
)
> new.df
species visit.time
1 A 2015-01-01 00:00:00
2 A 2015-01-01 00:02:00
3 A 2015-01-01 00:04:00
4 A 2015-01-01 00:06:00
5 A 2015-01-01 00:08:00
6 A 2015-01-01 00:10:00
7 B 2015-01-01 00:00:00
8 B 2015-01-01 00:02:00
9 B 2015-01-01 00:04:00
10 B 2015-01-01 00:06:00
11 B 2015-01-01 00:08:00
12 B 2015-01-01 00:10:00
I would like to create a new column called "visitID" that records an each species' visit that occured. However, I only want to assign a unique number only of the visit occurred at least 2 minutes after the previous recorded visit:
> new.df
species visit.time visitID
1 A 2015-01-01 00:00:00 1
2 A 2015-01-01 00:02:00 -
3 A 2015-01-01 00:04:00 2
4 A 2015-01-01 00:06:00 -
5 A 2015-01-01 00:08:00 3
6 A 2015-01-01 00:10:00 -
7 B 2015-01-01 00:00:00 1
8 B 2015-01-01 00:02:00 -
9 B 2015-01-01 00:04:00 2
10 B 2015-01-01 00:06:00 -
11 B 2015-01-01 00:08:00 3
12 B 2015-01-01 00:10:00 -
where -
is just an NA
I would usually try using dplyr:mutate
with conditional terms ifelse
, the problem is I do not know how to account for time elapse from the previous visit.
Please let me know if there are more details that could provide. Thanks!