Week 5

Demo

age <- function(birthday, output_unit){
  difftime(Sys.Date(), birthday, units = output_unit)
}

age("2000-10-02", "days")/365
Time difference of 24.4879 days

Task 1

# 1.
BM1 <- function(Weight, Height){
  BMI_output <- Weight/(Height^2)
  return(BMI_output) 
} #return output is often not needed, but can help in complex cases
BM1(80, 1.87)
[1] 22.87741
# 2.
Farenheight <- function(Temperature){Temperature*(9/5)+32}
Farenheight(20)
[1] 68
# 3.
eucledian_distance <- function(x1, y1, x2, y2){
  sqrt((x2-x1)^2 + (y2-y1)^2)
}
eucledian_distance(0, 0, 1, 1)
[1] 1.414214

Task 2

library("readr")
library("sf")
Linking to GEOS 3.12.2, GDAL 3.9.3, PROJ 9.4.1; sf_use_s2() is TRUE
library("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
library("ggplot2")
wildschwein <- read_delim("data/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.
# selecting animal & time span
Sabi <- wildschwein |>
  filter(
    TierName == "Sabi",
    DatetimeUTC >= "2015-04-01", 
    DatetimeUTC < "2015-04-15")
Rosa <- wildschwein |>
  filter(
    TierName == "Rosa",
    DatetimeUTC >= "2015-04-01", 
    DatetimeUTC < "2015-04-15")

Task 3

#taking  a look
head(Sabi)
# A tibble: 6 × 6
  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.
head(Rosa)
# A tibble: 6 × 6
  TierID TierName CollarID DatetimeUTC                E        N
  <chr>  <chr>       <dbl> <dttm>                 <dbl>    <dbl>
1 016A   Rosa        13972 2015-03-31 22:00:18 2570778. 1204985.
2 016A   Rosa        13972 2015-03-31 22:15:16 2570795. 1204957.
3 016A   Rosa        13972 2015-03-31 22:30:34 2570797. 1204928.
4 016A   Rosa        13972 2015-03-31 22:45:13 2570810. 1204914.
5 016A   Rosa        13972 2015-03-31 23:00:10 2570850. 1204876.
6 016A   Rosa        13972 2015-03-31 23:15:19 2570844. 1204864.
library(lubridate)

Attache Paket: 'lubridate'
Die folgenden Objekte sind maskiert von 'package:base':

    date, intersect, setdiff, union
# rounding time
Sabi <- Sabi |>
  mutate(Round_time = round_date(DatetimeUTC, "15 mins"))
Rosa <- Rosa |>
  mutate(Round_time = round_date(DatetimeUTC, "15 mins"))
# joining the two animals and making coordinates long format
Wildschwein_complete <- full_join(Sabi, Rosa, by = "Round_time", 
                                  suffix = c("_x", "_y")) 

Task 4

# calculation of distance
Wildschwein_complete <- Wildschwein_complete |>
  mutate(Eucledian_distance = eucledian_distance(
    E_x, N_x, E_y, N_y
  ))  
# checking if they met
Wildschwein_complete <- Wildschwein_complete |>
  mutate(meet = if_else(Eucledian_distance <= 100, TRUE, FALSE))

Task 5

# select data
Wildschwein_meet <- Wildschwein_complete |>
  filter(meet == TRUE)
# visualize, dots with black outlines are meeting points
ggplot(Rosa, aes(E, N)) +
  geom_point(aes(color = "Rosa"), alpha = 0.1) +  
  geom_point(data = Sabi, aes(color = "Sabi"), alpha = 0.1) +  
  geom_point(data = Wildschwein_meet, aes(E_x, N_x, fill = "Rosa"), 
             shape = 21, stroke = 1.2, size = 1.2, color = "black") +  
  geom_point(data = Wildschwein_meet, aes(E_y, N_y, fill = "Sabi"), 
             shape = 21, stroke = 1.2, size = 1.2, color = "black") +  
  scale_color_manual(values = c("Rosa" = "red", "Sabi" = "blue")) +  
  scale_fill_manual(values = c("Rosa" = "red", "Sabi" = "blue")) + 
  labs(color = "Locations", fill = "Meets") +
  theme(legend.position = "right")