# INBO CODING CLUB 24 September 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. ## Participants Name | Challenges --- | --- Damiano Oldoni | Dirk Maes | *** Emma Cartuyvels| *** Hans Van Calster | ** Wim Mertens | *** Jolien Goossens| *** Tanja Milotic| ** Luc De Bruyn | *** Suzanna Lettens |*** Patrik Oosterlynck|* Anja Leyman| *** Tine Bommarez| Pieterjan Verhelst |** joost vanoverbeke | ** Lien Reyserhove | Britt Lonneville | *** Nicolas Noé | * ## Challenge 1 Emma: ``` birds %>% filter(str_count(metal_ring) == 6) %>% filter(str_starts(color_ring, "C")) %>% filter(str_ends(color_ring, "R")) str_subset(birds$color_ring, "[:lower:]") birds <- birds %>% mutate(color_ring = str_to_upper(color_ring)) ``` Hans ``` birds %>% filter(str_length(metal_ring) == 6, str_detect(color_ring, pattern = "^C.+R$")) birds %>% filter(color_ring != str_to_upper(color_ring)) birds <- birds %>% mutate(color_ring = str_to_upper(color_ring)) ``` Wim ``` birds %>% filter(str_length(metal_ring) == 6, str_sub(color_ring, 1, 1) == "C", str_sub(color_ring, -1, -1) == "R") birds %>% filter(str_detect(color_ring, '[:lower:]')) birds <- birds %>% mutate(color_ring = str_to_upper(color_ring)) ``` Britt ``` birds$color_ring <- str_to_upper(birds$color_ring) subset(birds, str_length(birds$metal_ring) == 6 & str_sub(birds$color_ring, 1, 1) == "C" & str_sub(birds$color_ring, str_length(birds$color_ring), str_length(birds$color_ring)) == "R") ``` Jolien ``` birds %>% filter( str_length(metal_ring) == 6 & str_detect(color_ring, "^C\\w*Y$") ) birds %>% filter( str_detect(color_ring, "[:lower:]") ) birds %>% mutate(color_ring = str_to_upper(color_ring)) ``` ## Challenge 2 Emma : ``` # Challenge 2 birds <- birds %>% mutate(color_ring_complete = paste0(background_color, inscription_color, "(", color_ring, ")")) birds %>% filter(str_detect(color_ring, pattern = "\\w\\wA\\w")) ``` Hans ``` birds <- birds %>% mutate(color_ring_complete = str_c(background_color, inscription_color, "(", color_ring, ")")) birds %>% filter(str_detect(color_ring, "[:alpha:]{2}A[:alpha:]")) ``` Dirk (not correct because there are color-rings with numbers ...) ``` birds %>% filter(str_length(color_ring) == 4) %>% filter(str_sub(color_ring, start = 3, end = 3) == "A") ``` Joost (nog één criterium extra, Dirk), dank je Joost! ``` birds %>% filter(str_length(color_ring) == 4, str_count(color_ring, "[:alpha:]") == 4, str_sub(color_ring, 3, 3) == "A") ``` Jolien ``` #1 birds %>% mutate( color_ring_complete = glue::glue_data(birds, "{background_color}{inscription_color}({color_ring})") ) #2 birds %>% filter( str_length(color_ring) == 4 & str_locate(birds$color_ring, "A") ==3 ) %>% distinct(color_ring) ``` Tine ``` birds$color_ring_complete <- paste0(birds$background_color, birds$inscription_color,"(",birds$color_ring,")") birds %>% filter(str_length(color_ring) == 4) %>% filter(str_sub(color_ring,3,3) == "A") ``` Anja ``` t <- birds %>% mutate(color_ring_complete = str_c(background_color, inscription_color, "(", color_ring, ")", collapse = NULL)) t <- birds %>% filter(str_sub(color_ring, 3, 3) == "A") %>% filter(str_length(color_ring) == 4) %>% filter(str_detect(color_ring, "[:alpha:]")) ``` ## INTERMEZZO Regex compendium shown live: ``` example_string <- "I. love. the. 2019(!!). INBO. Coding. Club! Session. of. 24/09/2020...." # Extract all digits # option 1 str_extract_all(example_string, pattern = "[:digit:]") #option 2 str_extract_all(example_string, pattern = "[0-9]") # Remove all dots str_remove_all(example_string, pattern = "\\.+") a <- "zae123zae" str_remove_all(a, pattern = "^zae") # power of anchors!! # Remove the very last dot at the end str_remove_all(example_string, pattern = "\\.$") # anchor $ str_remove_all(example_string, pattern = "\\.+$") # anchor $ # Remove all extra dots at the end of the sentence = #dots preceeded by dots str_remove_all(example_string, pattern = "(?<=\\.)\\.") ``` ## Challenge 3 Emma: ``` birds %>% filter(color_ring != str_replace(color_ring_dots, "\\.", "")) birds <- birds %>% mutate(metal_ring = str_replace(metal_ring, "^\\*+", "")) birds %>% filter(str_detect(color_ring, pattern = "[AEIUOY]{2,}")) # optie 2 birds %>% filter(str_detect(color_ring, pattern = "[AEIUOY](?=[AEIUOY])")) ``` Jolien ``` birds %>% filter(color_ring != str_replace_all(color_ring_dots, "\\.", "")) birds %>% mutate(metal_ring = str_replace_all(metal_ring, "^\\*+\\w", "") ) ``` Anja ``` anomalies <- birds %>% filter(color_ring != str_remove_all(color_ring_dots, pattern = "\\.")) birds %>% mutate(metal_ring = str_remove(metal_ring, pattern = "^*+")) vowels <- birds %>% filter(str_detect(color_ring, pattern = "[aeiou](?=[aeiou])")) ``` Nicolas ``` birds %>% filter(color_ring != str_replace_all(color_ring_dots, "\\.", "")) birds %>% mutate(metal_ring = str_remove(metal_ring, pattern = "^*+")) ``` Wim ``` birds %>% filter(color_ring != str_remove_all(color_ring_dots, pattern = "\\.")) birds %>% mutate(metal_ring = str_remove_all(metal_ring, pattern = "^\\*+")) birds %>% filter(str_detect(color_ring, pattern = "[AEIOUY]{2,}")) ``` Britt ``` birds %>% filter(birds$color_ring != str_remove_all(birds$color_ring_dots, pattern = "\\.")) birds$metal_ring <- str_remove_all(birds$metal_ring, pattern = "^\\*{1,2}") birds %>% filter(str_detect(birds$color_ring, pattern = "[AEIOU](?=[AEIOU])")) ``` ## Bonus challenge Emma: ``` birds <- birds %>% mutate(dot_count = 10 - str_count(metal_ring)) %>% mutate(metal_ring_2 = str_replace(metal_ring, "(?<=[:alpha:])", str_dup(".", dot_count))) ``` Hans (with a little help from Emma ;-)): ``` birds %>% select(metal_ring_clean) %>% mutate( ndots <- 10 - str_length(metal_ring_clean), dots = str_dup(".", ndots), euring_code = str_c( str_extract(metal_ring_clean, "^[:upper:]+"), dots, str_extract(metal_ring_clean, "\\d+$") )) ```