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.
#175–176
Puzzles
Author: ExcelBI
All files (xlsx with puzzle and R with solution) for each and every puzzle are available on my Github. Enjoy.
Puzzle #175
Sometimes even common tables tell the story. Today we have table of people that are family, but we need to find their relationships. We only have first and last name and number of generation. So to find relations we need to make manouver called self-join. Let check it out.
(Note: there are some flaws in file so code has some cleaning fragments.)
Loading libraries and data
library(tidyverse) library(readxl) input = read_excel("Power Query/PQ_Challenge_175.xlsx", range = "A1:C16") test = read_excel("Power Query/PQ_Challenge_175.xlsx", range = "E1:H19") %>% mutate(Relantionship = str_remove_all(Relantionship, " ")) # cleaned for purpose of validation
Transformation
result = input %>% left_join(input, by = c("Family" = "Family")) %>% filter(`Generation No.x` == `Generation No.y` - 1) %>% # there is mispronunciation in the challenge, it should be "Relationship" not "Relantionship" unite("Relantionship", `Generation No.x`, `Generation No.y`, sep = "-") %>% select(Name = `Name.x`,Family,`Next Generation` = `Name.y`, Relantionship ) %>% arrange(Family, Relantionship , Name, `Next Generation`)
Validation
identical(result, test) # [1] TRUE
Puzzle #176
Probably somebody make a note by hand, not thinking of further users and as usually we need to straight issue up. From data concatenated in two columns of table we need to make long table with running sum per group. Go on, lets do it.
Loading libraries and data
library(tidyverse) library(readxl) input = read_excel("Power Query/PQ_Challenge_176.xlsx", range = "A1:C5") test = read_excel("Power Query/PQ_Challenge_176.xlsx", range = "E1:G9")
Transformation
result = input %>% mutate(Column1 = map(Column1, ~strsplit(.x, ", ")), Column2 = map(Column2, ~strsplit(.x, ", "))) %>% unnest(cols = c(Column1, Column2)) %>% mutate(Column1 = map(Column1, ~tibble(Column1 = .x)), Column2 = map(Column2, ~tibble(Column2 = .x))) %>% mutate(n1 = map_dbl(Column1, ~nrow(.x)), n2 = map_dbl(Column2, ~nrow(.x))) %>% mutate(Column = map2(Column1, Column2, ~{ n1 = nrow(.x) n2 = nrow(.y) if (n1 > n2) { .y = bind_rows(.y, tibble(Column2 = rep("0", n1 - n2))) } else if (n1 < n2) { .x = bind_rows(.x, tibble(Column1 = rep(NA, n2 - n1))) } bind_cols(.x, .y) })) %>% select(Group, Column) %>% unnest(cols = c(Column)) %>% drop_na() %>% mutate(Column2 = cumsum(as.numeric(Column2)), .by = Group)
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.