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.

#221–222

Puzzles

Author: ExcelBI

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

Puzzle #221

We already had task to provide indices for some structured project, and here we go again. Once again I found out that perfect concept to use for such puzzles is factorization. Using it properly make solution of this task nice and pretty short.

Loading libraries and data

library(tidyverse)
library(readxl)

path = "Power Query/PQ_Challenge_221.xlsx"
input = read_excel(path, range = "A1:C20")
test  = read_excel(path, range = "E1:J20")

Transformation

result = input %>%
  mutate(Project_Index = as.numeric(as.factor(Project))) %>%
  mutate(Task_Index = as.numeric(paste0(Project_Index,".",as.numeric(as.factor(Task)))) , .by = Project) %>%
  mutate(Activity_Index = paste0(Task_Index,".", as.numeric(as.factor(Activity))), .by = c(Project, Task))

Validation

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

Puzzle #222

Classic Power Query task — restructure table a little bit. And today we have list of exams scored by different students. But not all of them passed the exam, and we need to filter them out as well. There are several manouvers to follow and we get table with each student that passed the exam. It is pretty tricky one. So lets check it out.

Loading libraries and data:

library(tidyverse)
library(readxl)

path = "Power Query/PQ_Challenge_222.xlsx"
input = read_excel(path, range = "A1:I7")
test  = read_excel(path, range = "A11:D17")

Transformation

result = input %>%
  pivot_longer(cols = -c(1), names_to = c(".value", "Type"), names_pattern = "(\\D+)(\\d+)") %>%
  mutate(rank = rank(desc(Marks), ties.method = "first"), .by = Test) %>%
  select(-Type) %>%
  unite("TM", Student, Marks, sep = " ") %>%
  pivot_wider(names_from = rank, values_from = TM, names_prefix = "Student")  %>%
  select(Subjects = Test, sort(names(.)[-1])) %>%
  filter(!is.na(Subjects)) %>%
  mutate(across(2:ncol(.), ~ifelse(as.numeric(str_extract(., "\\d+")) >= 40, str_remove(., "\\s\\d+"), NA_character_))) %>%
  select(where(~!all(is.na(.)))) %>%
  arrange(Subjects) 

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)