owned this note
owned this note
Published
Linked with GitHub
# INBO CODING CLUB
27 October 2020
Welcome!
## Share your code snippet
If you want to share your code snippet, copy paste your snippet within a section of three backticks (```):
As an **example**:
```
library(tidyverse)
```
(*you can copy paste this example and add your code further down*)
## Yellow sticky notes
No yellow sticky notes online. Put your name + " | " and add a "*" each time you solve a challenge (see below).
## Participants
Name | Challenges
--- | ---
Damiano Oldoni |
Dirk Maes |
Joost Vanoverbeke | **
Hans Van Calster | **
Emma Cartuyvels |**
Suzanna Lettens |
Lynn Pallemaerts | **
Jim Casaer |
# Tips and tricks
It's always good to know which parsing format lubridate uses. You get to know it by
typing `options(lubridate.verbose = TRUE)` in your console once.
## Challenge 1
### Solution Dirk 1.1
```
datetimes_to_parse$datetime1c <- ymd_hms(datetimes_to_parse$datetime1)
datetimes_to_parse$datetime2c <- ydm_hms(datetimes_to_parse$datetime2)
datetimes_to_parse$datetime3c <- dmy_hm(datetimes_to_parse$datetime3)
```
Emma:
```
#1
datetimes_to_parse <- datetimes_to_parse ymd_hms
mutate(datetime1 = ymd_hms(datetime1),
datetime2 = ydm_hms(datetime2),
datetime3 = dmy_hm(datetime3))
#2
wday(c("2006-04-01","1985-07-17", "1991-03-13"), label = TRUE)
#3
sf <- stamp("The institute was founded on Monday, Jan 14, 1999",
locale = "English")
sf(ymd("2006-04-01"))
sf(ymd("1985-07-17"))
sf(ymd("1991-03-13"))
#4
difftime(ymd("1985-07-17"), ymd("2006-04-01"))
difftime("2006-04-01", "1991-03-13")
```
Joost:
```
#2
INBO_dates <- tibble(instituut = c("INBO", "IN", "IBW"),
datum = c("2006-04-01", "1985-07-17", "1991-03-13")) %>%
mutate(datum = ymd(datum),
dag = wday(datum,
label = TRUE,
abbr = FALSE,
week_start = 1))
#3
stmp <- stamp_date("Het instituut werd opgericht op maandag, 1 januari, 1900")
INBO_dates %>%
pull(datum) %>%
stmp()
#of iets fancier
INBO_dates %>%
{map2(.$instituut, .$datum,
function(x,y)
stamp(paste("Het instituut", x, "werd opgericht op maandag, 1 januari, 1900"))(y))}
```
## INTERMEZZO
UTC is the time standard and it is therefore used by default when defining a datetime
```
time_in_utc <- ymd_hms("2020-10-27 10:00:00")
time_in_utc
```
Showing differences between intervals and periods while daylight saving occurs:
```
start_datetime <- as_datetime("2020-10-25 01:00:00", tz = "CET")
# add a period (periods ignore time line irregularities)
start_datetime + hours(2)
# add a duration (track track the passage of physical time)
start_datetime + dhours(2)
end_datetime <- as_datetime("2020-10-25 03:00:00", tz = "CET")
end_datetime - start_datetime
```
**Periods** follow the everyday clock (with irregularities such as Daylight Saving Time), durations not as **durations** track the passage of physical time.
## Challenge 2
Joost
noot: om DST te verwijderen van tijd in CET
(vb langdurende tijdreeksen in CET **zonder** zomertijd)
```
as_datetime("2020-08-01 09:00:00", tz = "CET")
as_datetime("2020-08-01 09:00:00", tz = "CET") %>% with_tz("Etc/GMT-1")
as_datetime("2020-11-01 09:00:00", tz = "CET")
as_datetime("2020-11-01 09:00:00", tz = "CET") %>% with_tz("Etc/GMT-1")
```
Hans
```
# challenge 2.1
data_cameratrap <-
data_cameratrap %>%
mutate(image_sequence_datetime = as_datetime(image_sequence_datetime,
tz = "EET"))
# challenge 2.2
data_cameratrap <-
data_cameratrap %>%
mutate(camera_deployment_start = with_tz(camera_deployment_start,
tzone = "EET"),
camera_deployment_end = with_tz(camera_deployment_end,
tzone = "EET"))
```
Emma:
```
data_cameratrap <- data_cameratrap %>%
mutate(image_sequence_datetime = as_datetime(image_sequence_datetime,
tz = "Europe/Kiev"))
# CHALLENGE 2.2
data_cameratrap <-
data_cameratrap %>%
mutate(camera_deployment_start = force_tz(camera_deployment_start,
tzone = "Europe/Kiev"),
camera_deployment_end = force_tz(camera_deployment_end,
tzone = "Europe/Kiev"))
```
## Challenge 3
Hans
```
data_cameratrap <- data_cameratrap %>%
pivot_longer(cols = where(is.POSIXct),
names_to = "datevariables",
values_to = "datetimes") %>%
mutate(hour = hour(datetimes),
day = day(datetimes))
```