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.
library(sf)
Warning: Paket 'sf' wurde unter R Version 4.5.3 erstellt
Linking to GEOS 3.14.1, GDAL 3.12.1, PROJ 9.7.1; sf_use_s2() is TRUE
wildschwein_sf <-st_as_sf(wildschwein, coords=c("E", "N"), crs=2058, remove=FALSE) #to get geometry from coords, but still let coords appearlibrary(dplyr)
Attache Paket: 'dplyr'
Die folgenden Objekte sind maskiert von 'package:stats':
filter, lag
Die folgenden Objekte sind maskiert von 'package:base':
intersect, setdiff, setequal, union
Warning: Paket 'ggplot2' wurde unter R Version 4.5.3 erstellt
ggplot(sabi) +geom_sf() +geom_path(aes(E, N)) #specify at the end that axis are E and N, geom_path respects temporal order of data
Step a): Specifiy a temporal window v
#calculate 4 distances for each point
v is 60 minutes, our sampling interval is 15 min. So we need to calculate the following distances.
distance_by_element <-function(later, now) {as.numeric(st_distance(later, now, by_element =TRUE) #by_element=True, because otherwise all distances between all points are calculated, and not only between later and now )}sabi <- sabi |>mutate(nMinus2=distance_by_element(lag(geometry, n=2), geometry),nMinus1=distance_by_element(lag(geometry, n=1), geometry),nPlus1=distance_by_element(geometry, lead(geometry, n=1)),nPlus2=distance_by_element(geometry, lead(geometry, n=2)))
sabi <- sabi |>rowwise() |>#mean for each row,mutate(stepMean=mean(c(nMinus2, nMinus1, nPlus1, nPlus2), na.rn=FALSE) #mean of moving window of one point )
Step C) remove static points$
#first, specify treshold for asimple approach, wel use mean value as treshold value#find means that are lowed, to be removed (non-movement)treshold <-mean(sabi$stepMean, na.rm =TRUE)sabi <- sabi |>mutate(static=stepMean < treshold )|>ungroup()sabi
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.
library(sf)wildschwein_sf <-st_as_sf(wildschwein, coords=c("E", "N"), crs=2058, remove=FALSE) #to get geometry from coords, but still let coords appearlibrary(dplyr)Ruth <-filter(wildschwein_sf, TierName=="Ruth", DatetimeUTC >"2015-07-01", DatetimeUTC <"2015-07-03")library(ggplot2)ggplot(Ruth) +geom_sf() +geom_path(aes(E, N)) #specify at the end that axis are E and N, geom_path respects temporal order of data
Step a): Specifiy a temporal window v
#calculate 4 distances for each point
v is 60 minutes, our sampling interval is 15 min. So we need to calculate the following distances.
distance_by_element <-function(later, now) {as.numeric(st_distance(later, now, by_element =TRUE) #by_element=True, because otherwise all distances between all points are calculated, and not only between later and now )}Ruth <- Ruth |>mutate(nMinus2=distance_by_element(lag(geometry, n=2), geometry),nMinus1=distance_by_element(lag(geometry, n=1), geometry),nPlus1=distance_by_element(geometry, lead(geometry, n=1)),nPlus2=distance_by_element(geometry, lead(geometry, n=2)))
Ruth <- Ruth |>rowwise() |>#mean for each row,mutate(stepMean=mean(c(nMinus2, nMinus1, nPlus1, nPlus2), na.rn=FALSE) #mean of moving window of one point )
Explore
summary(Ruth$stepMean)
Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
1.677 5.898 20.312 82.202 90.998 760.357 4
ggplot(Ruth) +geom_path(aes(E, N, color = static, group =1)) +geom_point(aes(E, N)) #specify at the end that axis are E and N, geom_path respects temporal order of data
Define id
rle_id <-function(vec) { x <-rle(vec)$lengthsas.factor(rep(seq_along(x), times = x))}
Ruth <- Ruth |>mutate(segment_id =rle_id(static))
ggplot(Ruth) +geom_path(aes(E, N, group = segment_id, color = segment_id)) +geom_point(aes(E, N)) +theme_minimal()
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.