owned this note
owned this note
Published
Linked with GitHub
# INBO CODING CLUB
26 January 2023
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 | ***
Anja Leyman | **
Teun Everts |
Dirk Maes |
Wouter Depaepe |
Adriaan Seynaeve |
Raïsa Carmen| **
## Challenge 1
Example:
### Damiano's solution (dummy)
```
a <- 1 # dummy example
```
Hans:
````
## CHALLENGE 1
sf <- stamp_date("Occurrence recorded on Monday, Jan 23, 2023")
sf(ymd("2022-01-01"))
persian_hogweed <- persian_hogweed %>%
mutate(date = dmy(paste(day, month, year, sep = "-")),
weekday = wday(date, label = TRUE, locale = "en"),
date_stamp = sf(date),
label = sprintf(
"Occurrence of %s found in %s on %s, %s.",
species, countryCode, weekday, date))
````
````
persian_hogweed$Date <- make_date(year = persian_hogweed$year,
month = persian_hogweed$month,
day = persian_hogweed$day)
head(persian_hogweed)
````
### Jonas
````
# After reading the dataset persian_hogweed_2022_scandinavia.csv (code given), add a new column date to the data.frame of the type "Date" (see screenshot).
persian_hogweed_1 <- persian_hogweed %>%
mutate(
date = ymd(paste(year, month, day))
)
# Add a column called weekday with the day of the week, e.g. "Monday". Do you get week names in other language, e.g. Dutch? You probably need to specify the language "English" somewhere in the function :-) Tip: use day.html or the lubridate cheat sheet.
persian_hogweed_2 <- persian_hogweed_1 %>%
mutate(
weekday = wday(date, label = TRUE, abbr = FALSE, locale = "English")
)
# Use a stamp to create dates like this "Occurrence recorded on Monday, Jan 23, 2023". Put them in a new column called date_stamp.
stamp_function <- stamp("Occurrence recorded on Monday, Jan 23, 2023",
locale = "English",
orders = "amdy")
persian_hogweed_3 <- persian_hogweed_2 %>%
mutate(
date_stamp = paste0("Occurrence recorded on ",
weekday, ", ",
month(date, label = TRUE, abbr = TRUE), " ",
day(date), ", ",
year(date)),
date_stamp_2 = stamp_function(date)
)
# Add a column called label combining the values in columns species, countryCode and date in this way: "Occurrence of Heracleum persicum found in SE on Fri, 25-9-22.".
# WORK IN PROGRESS, still working on date format
persian_hogweed_4 <- persian_hogweed_3 %>%
mutate(
label = paste0("Occurrence of ",
species, " found in ",
countryCode, " on ",
weekday, ", ",
as.Date(date, format = "%d-%m-%y"), ".")
)
````
## Challenge 2
### Solution of Raisa
```
deployments <- deployments %>%
mutate(local_start = with_tz(start, tzone = "CET"),
local_end = with_tz(end, tzone = "CET"))
#fix the bug and calculate duration, day and hour
deployments <- deployments %>%
mutate(start = force_tz(start, tzone = "CET"),
end = force_tz(end, tzone = "CET"),
local_start = start,
local_end = end,
duration = local_end - local_start,
hour_start = hour(start),
day_start = day(start))
```
### Hans
```
deployments <- deployments %>%
mutate(
start = force_tz(start, tzone = "Europe/brussels"),
end = force_tz(end, tzone = "Europe/brussels"),
local_start = local_time(start, tz = "Europe/brussels"),
local_end = local_time(end, tz = "Europe/brussels"),
duration = as.duration(end - start),
hour_start = hour(start),
day_start = day(start))
```
### Jonas
````
deployments_solution <- deployments %>%
mutate(
local_start = with_tz(start, tzone = "Europe/Brussels"),
local_end = with_tz(end, tzone = "Europe/Brussels"),
start_corrected = force_tz(start, tz = "Europe/Brussels"),
end_corrected = force_tz(end, tz = "Europe/Brussels"),
duration_s = dseconds(interval(local_start, local_end)),
hour_start = hour(local_start),
day_start = day(local_start)
)
````
## Challenge 3
### Hans
```
finbif_occs <- persian_hogweed %>%
filter(str_detect(occurrenceID, "^http://tun\\.fi/")) %>%
mutate(authorship = str_extract(scientificName, sprintf("(?<=%s\\s)\\w*", species)),
internal_id = str_split_i(occurrenceID, "[\\:\\.\\/]", i = -1))
```
### Raïsa
```
finbif_occs <- persian_hogweed %>%
filter(str_starts(occurrenceID, "http://tun.fi/")) %>%
mutate(authorship = str_remove(scientificName, species),
stringl = length(str_split(occurrenceID, pattern = "[:|.|/]")),
internalID = str_split(occurrenceID, pattern = "[:|.|/]")[stringl]
) %>%
dplyr::select(-stringl)
#Hans' solution with str_split_i if preferable but I think my package is out of data; my R installation does not know str_split_i so I made a workaround.
```