[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.#175–176

PuzzlesAuthor: ExcelBI

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

Puzzle #175Sometimes 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 #176Probably 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.

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.Continue reading: PowerQuery Puzzle solved with R