PowerQuery Puzzle solved with R
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
#189–190
Puzzles
Author: ExcelBI
All files (xlsx with puzzle and R with solution) for each and every puzzle are available on my Github. Enjoy.
Puzzle #189
We have sequential report of some weird acceptance process. Unfortunatelly we don’t know the rules, and we are suppose to only clean report to more digestible form. We need to check if after any number there is message “Yes”, if so, this number passes, if not it fails. We need to clean sequence from message rows. Check it out.
Loading libraries and data
library(tidyverse) library(readxl) input = read_excel("Power Query/PQ_Challenge_189.xlsx", range = "A1:B11") test = read_excel("Power Query/PQ_Challenge_189.xlsx", range = "D1:F8")
Transformation
result = input %>% mutate(Result = if_else(lead(Code) == "Yes", "Pass", NA)) %>% mutate(Result = if_else(is.na(Result) & str_detect(Code, "\\d"), "Fail", Result)) %>% filter(!is.na(Result)) %>% mutate(Code = as.numeric(Code))
Validation
identical(result, test) # [1] TRUE
Puzzle #190
Wow, that’s the puzzle I like, mine in dirty (aka untidy) data to dig info we really need. It seems that somebody took data from system, and forgot any separators. Somehow colons survived. And knowing what data we need, we have to prepare mechanism that will get every needed chunk of text. Let’s go, let’s use some Regex.
Later I found out another, shorter solution, which will be also below.
Loading libraries and data
library(tidyverse) library(readxl) library(rebus) input = read_excel("Power Query/PQ_Challenge_190.xlsx", range = "A1:A3") test = read_excel("Power Query/PQ_Challenge_190.xlsx", range = "A6:E8")
Transformation
name_pattern = "Name:" %R% capture(one_or_more(WRD)) %R% "Org:" org_pattern = "Org:" %R% capture(one_or_more(WRD)) %R% "City:" city_pattern = "City:" %R% capture(one_or_more(WRD)) %R% "FromDate:" from_date_pattern = "FromDate:" %R% capture(one_or_more(WRD)) %R% "ToDate:" to_date_pattern = "ToDate:" %R% capture(one_or_more(WRD)) extract_and_space <- function(a, name_pattern) { extracted <- str_match(a, name_pattern) result <- extracted %>% pluck(2) %>% {if (is.na(.)) extracted %>% pluck(1) else .} %>% str_replace_all("([a-z])([A-Z])", "\\1 \\2") %>% str_replace_all("([A-Z])([A-Z][a-z])", "\\1 \\2") return(result) } result = input %>% mutate(Name = map_chr(Data, ~extract_and_space(.x, name_pattern)), Org = map_chr(Data, ~ str_match(.x, org_pattern) %>% pluck(2)), City = map_chr(Data, ~ extract_and_space(.x, city_pattern)), `From Date` = map_chr(Data, ~ str_match(.x, from_date_pattern) %>% pluck(2)), `To Date` = map_chr(Data, ~ str_match(.x, to_date_pattern) %>% pluck(2))) %>% mutate(`From Date` = ymd(`From Date`) %>% as.POSIXct(), `To Date` = ymd(`To Date`) %>% as.POSIXct()) %>% select(-Data)
Transformation v2
pattern = 'Name:(\\w+)Org:(\\w+)City:(\\w+)FromDate:(\\d+)ToDate:(\\d+)' result2 <- input %>% extract(Data, into = c("Name", "Org", "City", "From Date", "To Date"), regex = pattern, remove = FALSE) %>% mutate(across(c(`From Date`, `To Date`), ~ ymd(.x) %>% as.POSIXct())) %>% mutate(across(c(Name, City), ~ str_replace_all(.x, "([A-Z])", " \\1") %>% trimws(which = "left"))) %>% select(-Data)
Validation
identical(result, test) # [1] TRUE identical(result2, test) # [1] TRUE
Feel free to comment, share and contact me with advices, questions and your ideas how to improve anything. Contact me on Linkedin if you wish as well.
PowerQuery Puzzle solved with R was originally published in Numbers around us on Medium, where people are continuing the conversation by highlighting and responding to this story.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.