Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
#177–178
Puzzles
Author: ExcelBI
All files (xlsx with puzzle and R with solution) for each and every puzzle are available on my Github. Enjoy.
Puzzle #177
We are about to create reports for three students, each of them have to pass 3 exams for each subject. If even one of them has mark lower than 40, person fails the subject. Then we have to find their averages per person and rank acording to it. And make small adjustment in first columns, not to repeat names for every subject. Check the code.
Loading libraries and data
library(tidyverse) library(readxl) input = read_excel("Power Query/PQ_Challenge_177.xlsx", range = "A1:F10") test = read_excel("Power Query/PQ_Challenge_177.xlsx", range = "H1:M10")
Transformation
result = input %>% rowwise() %>% mutate(Result = if_else(any(c(Marks1, Marks2, Marks3) < 40), "Fail", "Pass"), total = Marks1 + Marks2 + Marks3) %>% ungroup() %>% mutate(average = mean(total), rn = row_number(), .by = Name) aux_rank = result %>% select(Name, average) %>% distinct() %>% mutate(Rank = rank(-average)) result2 = result %>% left_join(aux_rank, by = "Name") %>% select(Name, Classs, Subject, `Total Marks` = total, Result, Rank, rn) %>% mutate(Name = ifelse(rn == 1, Name, NA_character_), Classs = ifelse(rn == 1, Classs, NA_real_), Rank = ifelse(rn == 1, Rank, NA_integer_)) %>% select(-rn)
Validation
identical(result2, test) #> [1] TRUE
Puzzle #178
HR should track how people they hire are advancing in positions, get promotions, but also if they are moving to another department or employer. We had input table containing of records of such events. But we need it to be more readable. Lets pivot the data.
Loading libraries and data
library(tidyverse) library(readxl) input = read_excel("Power Query/PQ_Challenge_178.xlsx", range = "A1:E5") test = read_excel("Power Query/PQ_Challenge_178.xlsx", range = "H1:K5")
Transformation
result = input %>% pivot_longer(-Emp, names_to = "Change", values_to = "Value") %>% separate(Change, into = c("Type", "Change"), sep = " ") %>% pivot_wider(names_from = Type, values_from = Value) %>% drop_na() # but one of my co-solver asked, why didn't I use only one pivot_longer # check Anil Kumar Goyal's solution then. input %>% pivot_longer(-Emp, names_to = c(".value", "Change"), names_sep = " ") %>% na.omit()
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.
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.