PowerQuery Puzzle solved with R

[This article was first published on Numbers around us - Medium, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

#167–168

Puzzles

Author: ExcelBI

All files (xlsx with puzzle and R with solution) for each and every puzzle are available on my Github. Enjoy.

Puzzle #167

Today we have little bit messed up table with very important information. This table has information about vaccination and we need to check if patient was notified about it and if dose of vaccine was administered. Power Query challenges usually need more code so lets do it.

Load libraries and data

library(tidyverse)
library(readxl)

input = read_excel("Power Query/PQ_Challenge_167.xlsx", range = "A1:D14")
test  = read_excel("Power Query/PQ_Challenge_167.xlsx", range = "G1:K16")

Transformation

result = input %>%
  mutate(`Camp No` = parse_number(`Camp No`),
         group = cumsum(!is.na(`Camp No`)),
         group_name = ifelse(`Camp No` == group, Vaccine, NA_character_)) %>%
  fill(group_name) %>%
  filter(is.na(`Camp No`)) %>%
  select(Vaccine = group_name,
         Name = Vaccine, 
         `Notification Date` = `Notification Date`,
         `Administration Date` = `Administration Date`) %>%
  pivot_wider(names_from = Name, values_from = c(`Notification Date`, `Administration Date`)) %>%
  pivot_longer(cols = -Vaccine, names_to = "Name", values_to = "Date") %>%
  separate(Name, into = c("Event", "Name"), sep = "_", remove = TRUE) %>%
  pivot_wider(names_from = Event, values_from = Date) %>%
  arrange(desc(Vaccine), Name) %>%
  mutate(`Camp No` = row_number() %>% as.numeric(), .by = Vaccine) %>%
  relocate(`Camp No`, .before = Vaccine) %>%
  mutate(Notified = ifelse(is.na(`Notification Date`), "No", "Yes"),
         Administered = ifelse(is.na(`Administration Date`), ifelse(is.na(`Notification Date`),"NA", "No"), "Yes")) %>%
  select(-c(4,5))

Validation

identical(result, test)
# [1] TRUE

Puzzle #168

In bingo we cross drawn numbers on special card. Lets imagine that we need to make similar thing with letters. We have three stores that have or not some items, and we need to write them down alphabetically as one message (delimited by “/”). Let’s do it.

Load libraries and data

library(tidyverse)
library(readxl)

input = read_excel("Power Query/PQ_Challenge_168.xlsx", range = "A1:B10")
test  = read_excel("Power Query/PQ_Challenge_168.xlsx", range = "D1:E10")

Transformation

result = input %>%
  mutate(nr = row_number(), 
         chars = as.vector(Item) %>% sort() %>% list(), 
         .by = Store) %>%
  mutate(Item = map2_chr(chars, nr,
                     ~str_c(.x, collapse = "/") 
                     %>% str_sub(1, ifelse(.y > 1, .y*2-1, 1)))) %>%
  select(Store, Item) 

Validation

identical(result, 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.

To leave a comment for the author, please follow the link and comment on their blog: Numbers around us - Medium.

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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)