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)^2return(BMI)}BMI(55, 163)
[1] 20.70082
# function which converts degree in Celcius to Farenheight: Farenheight <-function(celcius){ Farenheight = celcius * (9/5) +32return(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)
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.
# 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
# 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,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
# 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()`).