Site icon R-bloggers

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.

#169–170

Puzzles

Author: ExcelBI

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

Puzzle #169

In todays challenge we have certain pattern to extract from given texts. As you may notice I really like string manipulation tasks and I love to make them solved using Regular Expresions. In this task we need to take those which: starts with capital letter, does not contain lower case letters, contains capital letters AND digits. Pretty interesting conditions, so lets find out the way.

Loading data and library

library(tidyverse)
library(readxl)

input = read_excel("Power Query/PQ_Challenge_169.xlsx", range = "A1:A8")
test = read_excel("Power Query/PQ_Challenge_169.xlsx", range = "C1:D8")

Transformation

pattern = ("\\b[A-Z](?=[A-Z0-9]*[0-9])[A-Z0-9]*\\b")

result = input %>%
  mutate(Codes = map_chr(String, ~str_extract_all(., pattern) %>% unlist() %>% 
                              str_c(collapse = ", "))) %>%
  mutate(Codes = if_else(Codes == "", NA_character_, Codes)) 

My Regexp can look weird so let me break the mystery down:

Validation

all.equal(test$Codes, result$Codes)
# [1] TRUE

Puzzle #170

Power Query Challenges are usually about transforming data from one form to another, sometimes only about the structure, sometimes data are transformed as well. This challenge is sales summary. From over 90 purchases we need to summarise total revenue for weekdays and weekends, and point the most and the less popular product within those transactions. Check it up.

Load libraries and data

library(tidyverse)
library(readxl)

input = read_excel("Power Query/PQ_Challenge_170.xlsx", range = "A1:C92")
test  = read_excel("Power Query/PQ_Challenge_170.xlsx", range = "E1:H3")

Transformation

result = input %>%
  mutate(week_part = ifelse(wday(Date) %in% c(1, 7), "Weekend", "Weekday")) %>%
  summarise(total = sum(Sale), 
            .by = c(week_part, Item)) %>%
  mutate(min = min(total),
         max = max(total),
         full_total = sum(total),
         .by = c(week_part)) %>%
  filter(total == min | total == max) %>%
  mutate(min_max = ifelse(total == min, "min", "max")) %>%
  select(-c(total, min, max)) %>%
  pivot_wider(names_from = min_max, values_from = Item, values_fn = list(Item = list)) %>%
  mutate(min = map_chr(min, ~paste(.x, collapse = ", ")),
         max = map_chr(max, ~paste(.x, collapse = ", "))) 

colnames(result) <- colnames(test)

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.
Exit mobile version