Defining trips based on points in or not in harbour

library(ramb)
library(sf)
library(tidyverse)
# example data -----------------------------------------------------------------
d <-
  dansk |> 
  arrange(vessel_id, time_stamp) |> 
  # find points in harbour
  st_as_sf(coords = c("lon", "lat"),
           crs = 4326,
           remove = FALSE) |> 
  # in harbour = 1, at sea = 0
  mutate(in_harbour = rb_points_in_polygons(geometry, dansk_harbours))
# Jeppe ------------------------------------------------------------------------
jeppe <-
  d |> 
  mutate(trip_id = ramb:::rb_trip_jepol(vessel_id, time_stamp, in_harbour))
# Einar ------------------------------------------------------------------------
einar <-
  d |> 
  group_by(vessel_id) |> 
  mutate(trip_id = rb_trip(in_harbour)) |> 
  ungroup()

jeppe |> 
  st_drop_geometry() |>
  count(trip_id, name = "jeppe") |> 
  full_join(einar |> 
              st_drop_geometry() |> 
              mutate(trip_id = paste0(vessel_id, "_", trip_id)) |> 
              count(trip_id, name = "einar")) |> 
  #filter(jeppe != einar) |> 
  mutate(dt = abs(jeppe - einar)) |> 
  count(dt)
#> # A tibble: 3 × 2
#>      dt     n
#>   <int> <int>
#> 1     0     1
#> 2     1    90
#> 3    NA   114