<- function() {}
testfun testfun()
NULL
class(testfun)
[1] "function"
<- function() {}
testfun testfun()
NULL
class(testfun)
[1] "function"
<- function() {
testfun print("this function does nothing")
}
testfun()
[1] "this function does nothing"
<- function(sometext) {
testfun print(sometext)
}
testfun(sometext = "this function does slightly more, but still not much")
[1] "this function does slightly more, but still not much"
class(testfun)
[1] "function"
You can add options to executable code like this
<- function(birthday, output_unit) {
my_age difftime(Sys.time(), birthday, units = output_unit)
}
my_age(birthday = "1997-04-23", output_unit = "days")
Time difference of 10193.48 days
my_age("1997-04-23", "days")
Time difference of 10193.48 days
<- function(birthday, output_unit = "days") {
my_age difftime(Sys.time(), birthday, units = output_unit)
}
# if not stated otherwise, our function uses the unit "days"
my_age("1997-04-23")
Time difference of 10193.48 days
# We can still overwrite units
my_age("1997-04-23", "hours")
Time difference of 244643.6 hours
<- function(weight, height) {
calculate_bmi = weight / (height^2)
bmi return(bmi)
}calculate_bmi(weight = 70, height = 1.75)
[1] 22.85714
<- function(celsius, farenheigt) {
calculate_farenheight = celsius*(9/5)+32
farenheight return(farenheight)
}calculate_farenheight(celsius=5)
[1] 41
= function(x1,y1,x2,y2) {
euclidean_distance sqrt((x2-x1)^2 + (y2-y1)^2)
}euclidean_distance(0,0,1,1)
[1] 1.414214
Subset of our wild boar data: Individuals Rosa and Sabi for 01.04.2015 - 15.04.2015 mit dem Dataset Wildschwein_BE_2056.csv
library("readr")
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
<- read_delim("wildschwein_BE_2056.csv", ",") wildschwein_BE
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.
= wildschwein_BE |>
subset_wildschwein filter(TierName %in% c("Sabi", "Rosa")) |>
filter(DatetimeUTC >= as.POSIXct("2015-04-01",tz = "UTC") & DatetimeUTC <= as.POSIXct("2015-04-15", tz = "UTC"))
Round the minutes of DatetimeUTC to a multiple of 15 and store the values in a new column.
library(lubridate)
Attache Paket: 'lubridate'
Die folgenden Objekte sind maskiert von 'package:base':
date, intersect, setdiff, union
$Datetime_rounded = round_date(subset_wildschwein$DatetimeUTC,
subset_wildschweinunit = "15 minutes",
)
Measure distance between concurrent locations
library(dplyr)
= split(subset_wildschwein, subset_wildschwein$TierName)
wildschwein_list = wildschwein_list[["Sabi"]]
wildschwein_sabi = wildschwein_list[["Rosa"]]
wildschwein_rosa
= inner_join(wildschwein_sabi,wildschwein_rosa,by="Datetime_rounded",suffix= c("_Sabi", "_Rosa"))
wildschwein_join
= wildschwein_join |>
wildschwein_join mutate(Euclidean_Distance = sqrt((E_Sabi - E_Rosa)^2 + (N_Sabi - N_Rosa)^2))
= wildschwein_join |>
wildschwein_join mutate(meet = ifelse(Euclidean_Distance <= 100,TRUE, FALSE))
library(ggplot2)
library(dplyr)
= wildschwein_join |>
filter_meets filter(meet == TRUE)
ggplot() +
geom_point(data=subset_wildschwein, aes(x = E, y = N, color=TierName)) +
geom_point(data=filter_meets, aes (x = E_Sabi, y = N_Sabi, shape = TierName_Sabi), color = "black") +
geom_point(data=filter_meets, aes (x = E_Rosa, y = N_Rosa, shape = TierName_Rosa), color = "black") +
labs(x = "E", y = "N", color = "Standorte", shape = "Treffpunkte") +
theme_minimal()