task_5

library(tidyverse)
Warning: Paket 'tidyverse' wurde unter R Version 4.4.3 erstellt
Warning: Paket 'ggplot2' wurde unter R Version 4.4.3 erstellt
Warning: Paket 'tidyr' wurde unter R Version 4.4.2 erstellt
Warning: Paket 'purrr' wurde unter R Version 4.4.3 erstellt
Warning: Paket 'dplyr' wurde unter R Version 4.4.3 erstellt
Warning: Paket 'lubridate' wurde unter R Version 4.4.3 erstellt
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   4.0.1     ✔ tibble    3.2.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.2.0     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)

Funktionen

Task 1

# function that calculates a persons BMI based on their height and weight:

BMI <- function(weight, height){
  BMI = weight/(height/100)^2
  return(BMI)
}

BMI(55, 163)
[1] 20.70082
# function which converts degree in Celcius to Farenheight: 

Farenheight <- function(celcius){
  Farenheight = celcius * (9/5) + 32
  return(Farenheight)
}

Farenheight(20)
[1] 68
# function which calculates the Euclidean distance between two sets of coordinates (x1, y1, x2, y2): 

euclid_dist <- function(x1, y1, x2, y2){
  d <- sqrt((x2 - x1)^2 + (y2 - y1)^2)
  return(d)
}

euclid_dist(0, 0, 3, 4)
[1] 5

Task 2

wildschwein <- read_delim("wildschwein_BE_2056 (2).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.
wildschwein 
# A tibble: 51,246 × 6
   TierID TierName CollarID DatetimeUTC                E        N
   <chr>  <chr>       <dbl> <dttm>                 <dbl>    <dbl>
 1 002A   Sabi        12275 2014-08-22 21:00:12 2570409. 1204752.
 2 002A   Sabi        12275 2014-08-22 21:15:16 2570402. 1204863.
 3 002A   Sabi        12275 2014-08-22 21:30:43 2570394. 1204826.
 4 002A   Sabi        12275 2014-08-22 21:46:07 2570379. 1204817.
 5 002A   Sabi        12275 2014-08-22 22:00:22 2570390. 1204818.
 6 002A   Sabi        12275 2014-08-22 22:15:10 2570390. 1204825.
 7 002A   Sabi        12275 2014-08-22 22:30:13 2570387. 1204831.
 8 002A   Sabi        12275 2014-08-22 22:45:11 2570381. 1204840.
 9 002A   Sabi        12275 2014-08-22 23:00:27 2570316. 1204935.
10 002A   Sabi        12275 2014-08-22 23:15:41 2570393. 1204815.
# ℹ 51,236 more rows
wildschwein_filter <- wildschwein |> 
  filter(
    TierName %in% c("Sabi", "Rosa"),
    DatetimeUTC >= as.POSIXct("2015-04-01 00:00:00", tz = "UTC"),
    DatetimeUTC <  as.POSIXct("2015-04-16 00:00:00", tz = "UTC")
  )

wildschwein_filter
# A tibble: 2,860 × 6
   TierID TierName CollarID DatetimeUTC                E        N
   <chr>  <chr>       <dbl> <dttm>                 <dbl>    <dbl>
 1 002A   Sabi        12275 2015-04-01 00:00:11 2570372. 1205313.
 2 002A   Sabi        12275 2015-04-01 00:15:22 2570309. 1205262.
 3 002A   Sabi        12275 2015-04-01 00:30:11 2570326. 1205248.
 4 002A   Sabi        12275 2015-04-01 00:45:16 2570315. 1205242.
 5 002A   Sabi        12275 2015-04-01 01:00:44 2570323. 1205237.
 6 002A   Sabi        12275 2015-04-01 01:15:17 2570320. 1205247.
 7 002A   Sabi        12275 2015-04-01 01:30:22 2570324. 1205237.
 8 002A   Sabi        12275 2015-04-01 01:45:47 2570307. 1205250.
 9 002A   Sabi        12275 2015-04-01 02:00:17 2570292. 1205262.
10 002A   Sabi        12275 2015-04-01 02:15:12 2570354. 1205297.
# ℹ 2,850 more rows

Task 3

# To compare Rosa and Sabis location we first need to match the two animals temporally. For that we can use a join, but need identical time stamps to serve as a join key. We therefore need to slightly adjust our time stamps to a common, current interval. 

# The task is therefore to round the minutes of DatetimeUTC to a multiple of 15 (00, 15, 30, 45) and store the values in a new column. You can use the lubridate function round_date() for this. 

wildschwein <- wildschwein_filter |> 
  mutate(DatetimeRound = round_date(DatetimeUTC, unit = "15 mins"))

wildschwein
# A tibble: 2,860 × 7
   TierID TierName CollarID DatetimeUTC                E        N
   <chr>  <chr>       <dbl> <dttm>                 <dbl>    <dbl>
 1 002A   Sabi        12275 2015-04-01 00:00:11 2570372. 1205313.
 2 002A   Sabi        12275 2015-04-01 00:15:22 2570309. 1205262.
 3 002A   Sabi        12275 2015-04-01 00:30:11 2570326. 1205248.
 4 002A   Sabi        12275 2015-04-01 00:45:16 2570315. 1205242.
 5 002A   Sabi        12275 2015-04-01 01:00:44 2570323. 1205237.
 6 002A   Sabi        12275 2015-04-01 01:15:17 2570320. 1205247.
 7 002A   Sabi        12275 2015-04-01 01:30:22 2570324. 1205237.
 8 002A   Sabi        12275 2015-04-01 01:45:47 2570307. 1205250.
 9 002A   Sabi        12275 2015-04-01 02:00:17 2570292. 1205262.
10 002A   Sabi        12275 2015-04-01 02:15:12 2570354. 1205297.
# ℹ 2,850 more rows
# ℹ 1 more variable: DatetimeRound <dttm>

##Task 4

# To measure the distance between concurrent locations, we need to follow the following steps. 

# 1. Split the wildschwein object into one data.frame per animal.

wildschwein_sabi <- wildschwein |> 
  filter(TierName == "Sabi")

wildschwein_sabi
# A tibble: 1,440 × 7
   TierID TierName CollarID DatetimeUTC                E        N
   <chr>  <chr>       <dbl> <dttm>                 <dbl>    <dbl>
 1 002A   Sabi        12275 2015-04-01 00:00:11 2570372. 1205313.
 2 002A   Sabi        12275 2015-04-01 00:15:22 2570309. 1205262.
 3 002A   Sabi        12275 2015-04-01 00:30:11 2570326. 1205248.
 4 002A   Sabi        12275 2015-04-01 00:45:16 2570315. 1205242.
 5 002A   Sabi        12275 2015-04-01 01:00:44 2570323. 1205237.
 6 002A   Sabi        12275 2015-04-01 01:15:17 2570320. 1205247.
 7 002A   Sabi        12275 2015-04-01 01:30:22 2570324. 1205237.
 8 002A   Sabi        12275 2015-04-01 01:45:47 2570307. 1205250.
 9 002A   Sabi        12275 2015-04-01 02:00:17 2570292. 1205262.
10 002A   Sabi        12275 2015-04-01 02:15:12 2570354. 1205297.
# ℹ 1,430 more rows
# ℹ 1 more variable: DatetimeRound <dttm>
wildschwein_rosa <- wildschwein |> 
  filter(TierName == "Rosa")

wildschwein_rosa
# A tibble: 1,420 × 7
   TierID TierName CollarID DatetimeUTC                E        N
   <chr>  <chr>       <dbl> <dttm>                 <dbl>    <dbl>
 1 016A   Rosa        13972 2015-04-01 00:00:10 2570823. 1204800.
 2 016A   Rosa        13972 2015-04-01 00:15:14 2570831. 1204794.
 3 016A   Rosa        13972 2015-04-01 00:30:11 2570842. 1204796.
 4 016A   Rosa        13972 2015-04-01 00:45:17 2570820. 1204803.
 5 016A   Rosa        13972 2015-04-01 01:00:44 2570829. 1204787.
 6 016A   Rosa        13972 2015-04-01 01:15:06 2570831. 1204767.
 7 016A   Rosa        13972 2015-04-01 01:30:13 2570825. 1204763.
 8 016A   Rosa        13972 2015-04-01 01:45:43 2570832. 1204782.
 9 016A   Rosa        13972 2015-04-01 02:00:18 2570844. 1204771.
10 016A   Rosa        13972 2015-04-01 02:15:16 2570825. 1204789.
# ℹ 1,410 more rows
# ℹ 1 more variable: DatetimeRound <dttm>
# 2. Join these datasets by the new DatetimeRound column created in the last task. The joined observations are temporally close. 

join <- full_join(wildschwein_rosa, wildschwein_sabi, by = "DatetimeRound")
join
# A tibble: 1,440 × 13
   TierID.x TierName.x CollarID.x DatetimeUTC.x            E.x      N.x
   <chr>    <chr>           <dbl> <dttm>                 <dbl>    <dbl>
 1 016A     Rosa            13972 2015-04-01 00:00:10 2570823. 1204800.
 2 016A     Rosa            13972 2015-04-01 00:15:14 2570831. 1204794.
 3 016A     Rosa            13972 2015-04-01 00:30:11 2570842. 1204796.
 4 016A     Rosa            13972 2015-04-01 00:45:17 2570820. 1204803.
 5 016A     Rosa            13972 2015-04-01 01:00:44 2570829. 1204787.
 6 016A     Rosa            13972 2015-04-01 01:15:06 2570831. 1204767.
 7 016A     Rosa            13972 2015-04-01 01:30:13 2570825. 1204763.
 8 016A     Rosa            13972 2015-04-01 01:45:43 2570832. 1204782.
 9 016A     Rosa            13972 2015-04-01 02:00:18 2570844. 1204771.
10 016A     Rosa            13972 2015-04-01 02:15:16 2570825. 1204789.
# ℹ 1,430 more rows
# ℹ 7 more variables: DatetimeRound <dttm>, TierID.y <chr>, TierName.y <chr>,
#   CollarID.y <dbl>, DatetimeUTC.y <dttm>, E.y <dbl>, N.y <dbl>
# 3. In the joined dataset, calculate Euclidean distance between concurrent observations and store the values in a new column 

join <- join |> 
  mutate(
    euclid_dist = sqrt((E.y-E.x)^2 + 
                         (N.y-N.x)^2)
  )

View(join)

# 4. Use a reasonable threshold on distance to determine if the animals are also spatially close enough to constitute a meet (we use 100 meters). Store this Boolean information (True/False) in a new column. 

join <- join |> 
  mutate(Meet = euclid_dist < 100)

View(join)

join_filter <- join |> 
  filter(Meet == TRUE)

join_filter
# A tibble: 11 × 15
   TierID.x TierName.x CollarID.x DatetimeUTC.x            E.x      N.x
   <chr>    <chr>           <dbl> <dttm>                 <dbl>    <dbl>
 1 016A     Rosa            13972 2015-04-02 04:45:15 2570399. 1205393.
 2 016A     Rosa            13972 2015-04-02 05:00:12 2570395. 1205393.
 3 016A     Rosa            13972 2015-04-02 18:00:17 2570396. 1205388.
 4 016A     Rosa            13972 2015-04-02 18:30:13 2570350. 1205396.
 5 016A     Rosa            13972 2015-04-03 04:00:15 2570394. 1205391.
 6 016A     Rosa            13972 2015-04-03 04:15:10 2570395. 1205397.
 7 016A     Rosa            13972 2015-04-03 04:45:11 2570396. 1205396.
 8 016A     Rosa            13972 2015-04-03 05:45:44 2570393. 1205398.
 9 016A     Rosa            13972 2015-04-03 06:00:15 2570387. 1205402.
10 016A     Rosa            13972 2015-04-03 06:15:09 2570392. 1205398.
11 016A     Rosa            13972 2015-04-03 06:30:14 2570392. 1205390.
# ℹ 9 more variables: DatetimeRound <dttm>, TierID.y <chr>, TierName.y <chr>,
#   CollarID.y <dbl>, DatetimeUTC.y <dttm>, E.y <dbl>, N.y <dbl>,
#   euclid_dist <dbl>, Meet <lgl>

##Task 5

# Now visualize the meets spatially in a way that you think reasonable. For example in the plot as shows below. 


ggplot() +
  geom_point(data = wildschwein_sabi, aes(x = E, y = N, color = TierName)) + 
  geom_point(data = wildschwein_rosa, aes(x = E, y = N, color = TierName)) + 
  geom_point(data = join_filter, aes(E.x, N.x), shape = 21) + 
  geom_point(data = join_filter, aes(E.y, N.y), shape = 21) + 
  scale_x_continuous(limits = c(2570000, 2571000)) + 
  scale_y_continuous(limits = c(1204000, 1206000))
Warning: Removed 940 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 373 rows containing missing values or values outside the scale range
(`geom_point()`).