# task_1.R
################################################################################
<- function(height_m, weight_kg){
bmi / height_m^2
weight_kg
}
<- function(celcius){
celcius2farenheit <- celcius * 9/5 + 32
farenheit
# this function has an expicit return
return(farenheit)
}
<- function(x, y, n = 1) {
euclid <- sqrt((x - lead(x, n))^2 + (y - lead(y, n))^2)
distance
# this is an implicit return
distance
}
# with R's new shorthand:
<- \(x,y,n = 1) sqrt((x - lead(x, n))^2 + (y - lead(y, n))^2)
euclid
# task_2.R
################################################################################
library("readr") # move this to the top of your script
library("dplyr") # move this to the top of your script
<- read_delim("datasets/wildschwein_BE_2056.csv", ",")
wildschwein
<- wildschwein |>
wildschwein_filter filter(
>= as.POSIXct("2015-04-01 00:00:00",tz = "UTC"),
DatetimeUTC <= as.POSIXct("2015-04-15 23:59:59",tz = "UTC")
DatetimeUTC |>
) 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
<- wildschwein_filter |>
sabi filter(TierName == "Sabi")
<- wildschwein_filter |>
rosa filter(TierName == "Rosa")
<- full_join(sabi, rosa, by = c("DatetimeRound"), suffix = c("_sabi", "_rosa"))
wildschwein_join
<- 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_join |>
wildschwein_meet 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
################################################################################
<- wildschwein_join |>
meanmeetpoints 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")
))
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.