Rows: 51246 Columns: 6
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (2): TierID, TierName
dbl (3): CollarID, E, N
dttm (1): DatetimeUTC
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 51246 Columns: 6
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (2): TierID, TierName
dbl (3): CollarID, E, N
dttm (1): DatetimeUTC
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
distance_by_element <-function(later, now) {as.numeric(st_distance(later, now, by_element =TRUE))}Rosa <- Rosa |>mutate(nMinus2 =distance_by_element(lag(geometry, 2), geometry), # distance to pos -30 minutesnMinus1 =distance_by_element(lag(geometry, 1), geometry), # distance to pos -15 minutesnPlus1 =distance_by_element(geometry, lead(geometry, 1)), # distance to pos +15 mintuesnPlus2 =distance_by_element(geometry, lead(geometry, 2)) # distance to pos +30 minutes )
Rosa <- Rosa |>rowwise() |>mutate(stepMean =mean(c(nMinus2, nMinus1, nPlus1, nPlus2)) ) |>ungroup()ggplot(Rosa, aes(y = stepMean)) +geom_boxplot() #stepMean of 30 seams to be a fitting threshold
Warning: Removed 4 rows containing non-finite outside the scale range
(`stat_boxplot()`).
Rosa <- Rosa |>mutate(static_1 = stepMean <mean(stepMean, na.rm =TRUE)) |>mutate(static_2 = stepMean <50)Rosa_filter_1 <- Rosa |>#Threshold 1filter(!static_1)Rosa_filter_2 <- Rosa |>#Threshold 2filter(!static_2)p1 <- Rosa_filter_1 |>ggplot(aes(E, N)) +geom_point(data = Rosa, col ="red") +geom_path() +geom_point() +coord_fixed() +theme(legend.position ="bottom")p2 <- Rosa_filter_2 |>ggplot(aes(E, N)) +geom_point(data = Rosa, col ="red") +geom_path() +geom_point() +coord_fixed() +theme(legend.position ="bottom")library(patchwork)p1 + p2 #Differences between 2 thresholds
rle_id <-function(vec) { x <-rle(vec)$lengthsas.factor(rep(seq_along(x), times = x))}Rosa_filter <- Rosa_filter_1 |>mutate(segment_id =rle_id(static_1))ggplot(data = Rosa_filter, aes(E, N)) +geom_point(data = Rosa, col ="red") +geom_path() +#path without color, to connect different segmentsgeom_path(aes(color = segment_id)) +#color for the pathgeom_point(aes(color = segment_id)) +coord_fixed() +theme(legend.position ="bottom")
Exercise B
pedestrian <-read_delim("data/pedestrian.csv")
Rows: 289 Columns: 4
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
dbl (3): TrajID, E, N
dttm (1): DatetimeUTC
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.