+ "markdown": "## Solutions\n\n:::{.callout-tip}\nHover 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.\n:::\n\n::: {.cell}\n::: {.cell-output .cell-output-stdout}\n\n```{.sourceCode .r}\n\n\nbmi <- function(height_m, weight_kg){\n weight_kg / height_m^2\n}\n\ncelcius2farenheit <- function(celcius){\n farenheit <- celcius * 9/5 + 32\n \n # this function has an expicit return\n return(farenheit) \n}\n\n\neuclid <- function(x, y, n = 1) {\n distance <- sqrt((x - lead(x, n))^2 + (y - lead(y, n))^2)\n \n # this is an implicit return\n distance\n}\n\n# with R's new shorthand:\n\neuclid <- \\(x,y,n = 1) sqrt((x - lead(x, n))^2 + (y - lead(y, n))^2)\n```\n\n\n:::\n\n::: {.cell-output .cell-output-stdout}\n\n```{.sourceCode .r}\n\nlibrary(\"readr\") # move this to the top of your script\nlibrary(\"dplyr\") # move this to the top of your script\n\nwildschwein <- read_delim(\"datasets/wildschwein_BE_2056.csv\", \",\")\n\nwildschwein_filter <- wildschwein |>\n filter(\n DatetimeUTC >= as.POSIXct(\"2015-04-01 00:00:00\",tz = \"UTC\"),\n DatetimeUTC <= as.POSIXct(\"2015-04-15 23:59:59\",tz = \"UTC\")\n ) |>\n filter(TierName %in% c(\"Rosa\", \"Sabi\"))\n```\n\n\n:::\n\n::: {.cell-output .cell-output-stdout}\n\n```{.sourceCode .r}\nwildschwein_filter <- wildschwein_filter |>\n group_by(TierID) |>\n mutate(\n DatetimeRound = lubridate::round_date(DatetimeUTC, \"15 minutes\")\n )\n\nhead(wildschwein_filter)\n```\n\n\n:::\n\n::: {.cell-output .cell-output-stdout}\n\n```{.sourceCode .r}\nlibrary(\"purrr\") # move this to the top of your script\n\nsabi <- wildschwein_filter |>\n filter(TierName == \"Sabi\")\n\nrosa <- wildschwein_filter |>\n filter(TierName == \"Rosa\")\n\nwildschwein_join <- full_join(sabi, rosa, by = c(\"DatetimeRound\"), suffix = c(\"_sabi\", \"_rosa\"))\n\nwildschwein_join <- wildschwein_join |>\n mutate(\n distance = sqrt((E_rosa - E_sabi)^2 + (N_rosa - N_sabi)^2),\n meet = distance < 100\n )\n```\n\n\n:::\n\n::: {.cell-output .cell-output-stdout}\n\n```{.sourceCode .r}\nlibrary(\"ggplot2\") # move this to the top of your script\n\n\nwildschwein_meet <- wildschwein_join |>\n filter(meet)\n\nggplot(wildschwein_meet) +\n geom_point(data = sabi, aes(E, N, colour = \"sabi\"), shape = 16, alpha = 0.3) +\n geom_point(data = rosa, aes(E, N, colour = \"rosa\"), shape = 16, alpha = 0.3) +\n geom_point(aes(x = E_sabi, y = N_sabi, fill = \"sabi\"), shape = 21) +\n geom_point(aes(E_rosa, N_rosa, fill = \"rosa\"), shape = 21) +\n labs(color = \"Regular Locations\", fill = \"Meets\") +\n coord_equal() +\n theme_minimal()\n```\n\n\n:::\n\n::: {.cell-output .cell-output-stdout}\n\n```{.sourceCode .r}\nmeanmeetpoints <- wildschwein_join |>\n filter(meet) |>\n mutate(\n E.mean = (E_rosa + E_sabi) / 2,\n N.mean = (N_rosa + N_sabi) / 2\n )\n\nlibrary(\"plotly\") # move this to the top of your script\n\n\n# plot_ly(wildschwein_join, x = ~E_rosa, y = ~N_rosa, z = ~DatetimeRound, type = \"scatter3d\", mode = \"lines\") |>\n# add_trace(wildschwein_join, x = ~E_sabi, y = ~N_sabi, z = ~DatetimeRound) |>\n# add_markers(data = meanmeetpoints, x = ~E.mean, y = ~N.mean, z = ~DatetimeRound) |>\n# layout(scene = list(\n# xaxis = list(title = \"E\"),\n# yaxis = list(title = \"N\"),\n# zaxis = list(title = \"Time\")\n# ))\n\n\nwildschwein_join |>\n filter(DatetimeRound < \"2015-04-04\") |>\n plot_ly(x = ~E_rosa, y = ~N_rosa, z = ~DatetimeRound, type = \"scatter3d\", mode = \"lines\") |>\n add_trace(wildschwein_join, x = ~E_sabi, y = ~N_sabi, z = ~DatetimeRound) |>\n add_markers(data = meanmeetpoints, x = ~E.mean, y = ~N.mean, z = ~DatetimeRound) |>\n layout(scene = list(\n xaxis = list(title = \"E\"),\n yaxis = list(title = \"N\"),\n zaxis = list(title = \"Time\")\n ))\n```\n\n\n:::\n:::\n",
0 commit comments