Solutions

Tip

Hover over the code and copy the content by clicking on the clipboard icon on the top right. You can now paste this into an R-Script.

# task_1.R
################################################################################




bmi <- function(height_m, weight_kg){
  weight_kg / height_m^2
}

celcius2farenheit <- function(celcius){
  farenheit  <- celcius * 9/5 + 32
  
  # this function has an expicit return
  return(farenheit) 
}


euclid <- function(x, y, n = 1) {
  distance <- sqrt((x - lead(x, n))^2 + (y - lead(y, n))^2)
  
  # this is an implicit return
  distance
}

# with R's new shorthand:

euclid <- \(x,y,n = 1) sqrt((x - lead(x, n))^2 + (y - lead(y, n))^2)




# task_2.R
################################################################################



library("readr")  # move this to the top of your script
library("dplyr")  # move this to the top of your script

wildschwein <- read_delim("datasets/wildschwein_BE_2056.csv", ",")

wildschwein_filter <- wildschwein |>
       filter(
              DatetimeUTC >= as.POSIXct("2015-04-01 00:00:00",tz = "UTC"),
              DatetimeUTC <= as.POSIXct("2015-04-15 23:59:59",tz = "UTC")
       ) |>
       filter(TierName %in% c("Rosa", "Sabi"))




# task_3.R
################################################################################


wildschwein_filter <- wildschwein_filter |>
  group_by(TierID) |>
  mutate(
    DatetimeRound = lubridate::round_date(DatetimeUTC, "15 minutes")
  )

head(wildschwein_filter)




# task_4.R
################################################################################


library("purrr") # move this to the top of your script

sabi <- wildschwein_filter |>
  filter(TierName == "Sabi")

rosa <- wildschwein_filter |>
  filter(TierName == "Rosa")

wildschwein_join <- full_join(sabi, rosa, by = c("DatetimeRound"), suffix = c("_sabi", "_rosa"))

wildschwein_join <- wildschwein_join |>
  mutate(
    distance = sqrt((E_rosa - E_sabi)^2 + (N_rosa - N_sabi)^2),
    meet = distance < 100
  )




# task_5.R
################################################################################


library("ggplot2")  # move this to the top of your script


wildschwein_meet <- wildschwein_join |>
  filter(meet)

ggplot(wildschwein_meet) +
  geom_point(data = sabi, aes(E, N, colour = "sabi"), shape = 16, alpha = 0.3) +
  geom_point(data = rosa, aes(E, N, colour = "rosa"), shape = 16, alpha = 0.3) +
  geom_point(aes(x = E_sabi, y = N_sabi, fill = "sabi"), shape = 21) +
  geom_point(aes(E_rosa, N_rosa, fill = "rosa"), shape = 21) +
  labs(color = "Regular Locations", fill = "Meets") +
  coord_equal() +
  theme_minimal()




# task_6.R
################################################################################


meanmeetpoints <- wildschwein_join |>
  filter(meet) |>
  mutate(
    E.mean = (E_rosa + E_sabi) / 2,
    N.mean = (N_rosa + N_sabi) / 2
  )

library("plotly")  # move this to the top of your script


# plot_ly(wildschwein_join, x = ~E_rosa, y = ~N_rosa, z = ~DatetimeRound, type = "scatter3d", mode = "lines") |>
#   add_trace(wildschwein_join, x = ~E_sabi, y = ~N_sabi, z = ~DatetimeRound) |>
#   add_markers(data = meanmeetpoints, x = ~E.mean, y = ~N.mean, z = ~DatetimeRound) |>
#   layout(scene = list(
#     xaxis = list(title = "E"),
#     yaxis = list(title = "N"),
#     zaxis = list(title = "Time")
#   ))


wildschwein_join |>
  filter(DatetimeRound < "2015-04-04") |>
  plot_ly(x = ~E_rosa, y = ~N_rosa, z = ~DatetimeRound, type = "scatter3d", mode = "lines") |>
  add_trace(wildschwein_join, x = ~E_sabi, y = ~N_sabi, z = ~DatetimeRound) |>
  add_markers(data = meanmeetpoints, x = ~E.mean, y = ~N.mean, z = ~DatetimeRound) |>
  layout(scene = list(
    xaxis = list(title = "E"),
    yaxis = list(title = "N"),
    zaxis = list(title = "Time")
  ))