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.

```library(tidyverse)

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:

• \\b stands for word boundaries, we want to extract parts of text that do not have whitespaces or punctuation. Even if they are not linguistical words :).
• [A-Z] at the beginning means that first character must be a letter (between A and Z). If we would have to analyse whole text with spaces and everything we would use ^ to emphasize beginning of string, not word.
• At the end we have [A-Z0–9]* that stands for zero or more digits OR capital letters.
• Last part placed in the middle is called positive lookahead. (?=[A-Z0–9]*[0–9]) make us sure that these zero or more characters from previous point, where preceded with optional letters or digits [A-Z0–9]*and mandatory digits [0–9] . All together means that our string has at least capital letter at the beginning and one digit at the end.

#### 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.

```library(tidyverse)

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.