Demo

1 Create function

  1. BMI
  2. Celcius to Farenheit
  3. Euclidean Distance
Show/Hide Code
bmi <- function(weight_kg, height_m) {
  bmi_output <- weight_kg/height_m^2
  
  return(bmi_output)
}

bmi(70,1.7)
[1] 24.22145
Show/Hide Code
ctf <- function(celcius){
  celcius * 9/5 + 32
}
  
ctf(30)
[1] 86
Show/Hide Code
euclidean_distance <- function (x1,y1,x2,y2){sqrt((x2-x1)^2 + (y2-y1)^2)
}

euclidean_distance(0,0,1,1)
[1] 1.414214

##Prepare Analysis import library

Show/Hide Code
library("readr")
library("sf")
Warning: package 'sf' was built under R version 4.3.3
Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
Show/Hide Code
library("dplyr")

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
Show/Hide Code
library("lubridate")

Attaching package: 'lubridate'
The following objects are masked from 'package:base':

    date, intersect, setdiff, union

Import data and filter

Show/Hide Code
wildschwein <- read_delim("Datasets-20250312/wildschwein_BE_2056.csv")
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.
Show/Hide Code
#filter data "Rosa" & "Sabi" in the time 01.04.2015 - 15.04.2015

wildschwein <- wildschwein |>
    filter(
      TierName %in% c("Sabi", "Rosa"), 
      DatetimeUTC >= "2015-04-01", 
      DatetimeUTC < "2015-04-15"
      )

2 Create a Join Key

  1. round the minutes of DatetimeUTC
  2. store the value in a new column
Show/Hide Code
#1. 

wildschwein <- wildschwein |> 
  mutate(rounded_time = round_date(DatetimeUTC, unit = "15 minutes"))

wildschwein
# A tibble: 2,688 × 7
   TierID TierName CollarID DatetimeUTC                E        N
   <chr>  <chr>       <dbl> <dttm>                 <dbl>    <dbl>
 1 002A   Sabi        12275 2015-03-31 22:00:28 2570296. 1205283.
 2 002A   Sabi        12275 2015-03-31 22:15:44 2570259. 1205259.
 3 002A   Sabi        12275 2015-03-31 22:30:44 2570255. 1205259.
 4 002A   Sabi        12275 2015-03-31 22:46:04 2570245. 1205268.
 5 002A   Sabi        12275 2015-03-31 23:00:17 2570364. 1205314.
 6 002A   Sabi        12275 2015-03-31 23:15:12 2570375. 1205320.
 7 002A   Sabi        12275 2015-03-31 23:30:08 2570411. 1205347.
 8 002A   Sabi        12275 2015-03-31 23:45:13 2570429. 1205361.
 9 002A   Sabi        12275 2015-04-01 00:00:11 2570372. 1205313.
10 002A   Sabi        12275 2015-04-01 00:15:22 2570309. 1205262.
# ℹ 2,678 more rows
# ℹ 1 more variable: rounded_time <dttm>

3 Measuring distance at concurrent locations

  1. Split the wildschwein object into one data.frame per animal
Show/Hide Code
rosa <- wildschwein |>
    filter(
      TierName == "Rosa")

sabi <- wildschwein  |>
    filter(
      TierName == "Sabi")
  1. Join these datasety by the new Datetime
Show/Hide Code
joined <- full_join(rosa,sabi, by = "rounded_time", suffix = c("rosa","sabi"))