<- function() {}
testfun testfun()
NULL
class(testfun)
[1] "function"
Demo
<- 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"
<- function(birthday, output_unit) {
my_age difftime(Sys.time(), birthday, units = output_unit)
}
my_age(birthday = "1996-09-02", output_unit = "days")
Time difference of 10430.73 days
my_age("1997-04-23", "days")
Time difference of 10197.73 days
<- function(birthday="0000-01-01", 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 10197.73 days
#jesus would be
my_age() #verry old
Time difference of 739699.7 days
# We can still overwrite units
my_age("1997-04-23", "hours")
Time difference of 244745.5 hours
<- function(weight, height){
bmi /(height)^2
weight
}bmi(weight=75,height=1.79) #bmi = 23.4
[1] 23.40751
<- function(celsius=0){
fahrenheit *9/5)+32
(celsius
}fahrenheit(celsius=25) #fahrenheit = 68
[1] 77
<- function(x1, y1, x2, y2) {
euc_dist sqrt((x2 - x1)^2 + (y2 - y1)^2)
}euc_dist(x1=2,y1=3,x2=4,y2=5)
[1] 2.828427
library("dplyr")
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
library("tidyr")
library("readr")
<- read_delim("wildschwein_BE_2056.csv", delim=",") 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 |>
wildschw_filter filter(TierName %in% c("Sabi", "Rosa") &
>= as.POSIXct("2015-04-01 00:00:00", tz="UTC") &
DatetimeUTC <= as.POSIXct("2015-04-15 23:59:59", tz="UTC")) DatetimeUTC
library("lubridate")
Attaching package: 'lubridate'
The following objects are masked from 'package:base':
date, intersect, setdiff, union
<- wildschw_filter |>
wildschw_rounded mutate(RoundedDatetime = round_date(DatetimeUTC, "15 minutes"))
<- wildschw_rounded |> filter(TierName == "Rosa")
wildschw_rosa <- wildschw_rounded |> filter(TierName == "Sabi") wildschw_sabi
<- inner_join(wildschw_rosa, wildschw_sabi, by = "RoundedDatetime", suffix = c("_Rosa", "_Sabi")) wildschw_join
<- wildschw_join |>
euc_dist mutate(
distance = sqrt((E_Rosa - E_Sabi)^2 + (N_Rosa - N_Sabi)^2),
meet = distance <= 100 # TRUE if the distance is less than or equal to 100 meters
)
<- euc_dist |>
meets filter(meet == TRUE)
library(ggplot2)
|>
wildschw_rosa ggplot(aes(E,N)) +
geom_point(color="lightblue",alpha=0.4) +
geom_point(data=wildschw_sabi,color="orchid",alpha=0.4)+
geom_point(data=meets,aes(E_Sabi,N_Sabi),color="red")+
geom_point(data=meets,aes(E_Rosa,N_Rosa),color="blue")+
coord_fixed()
or with legend: (with help from ChatGPT)
library(ggplot2)
library(ggnewscale)
ggplot() +
# Regular locations
geom_point(data = wildschw_rosa, aes(E, N, color = "rosa"), alpha = 0.4) +
geom_point(data = wildschw_sabi, aes(E, N, color = "sabi"), alpha = 0.4) +
scale_color_manual(
name = "Regular Locations",
values = c("rosa" = "lightblue", "sabi" = "orchid")
+
) # Add new color scale for meets
new_scale_color() +
geom_point(data = meets, aes(E_Sabi, N_Sabi, color = "sabi"), size = 2) +
geom_point(data = meets, aes(E_Rosa, N_Rosa, color = "rosa"), size = 2) +
scale_color_manual(
name = "Meets",
values = c("rosa" = "blue", "sabi" = "red")) +
coord_fixed(
xlim = c(2569500, 2571200),
ylim = c(1204300, 1205800))