I have a dataset with GPS locations of birds doing multiple trips from a colony. I want to remove all points within 3 km from the colony, but ONLY those that are at the beginning or the end of an individual trip (Unique_id). If they come to within 3 km of the colony in the middle of a trip, and then head off again without returning to the colony first, I want to retain these points.
I calculated the distance to the colony and then defined using a logical column whether the location was < 3km (1) or > 3km (0). With the coordinates removed, the dataframe looks a bit like the below dummy data. So from here, I am looking to define something along the lines of "remove rows with dist3k == 1 for the first or last consecutive "1"s of a Unique_id.
Hope this makes sense, and look forward to suggestions.
# what the data looks like
data_orig <- data.frame(
Index = rep(c('1','2','3','4','5','6','7','8','9','10',
'11','12','13','14','15','16','17','18','19','20')),
Unique_id = rep(c('A1','A2'), each = 10),
dist3k = rep(c('1','1','0','0','0','1','1','0','0','1','1','1','1','0','1','0','1','0','1','1')))
# what I want the output to be
data_new <-data_orig[-c(1,2,10,11,12,13,19,20),]