Want to share your content on R-bloggers? click here if you have a blog, or here if you don’t.
#171–172
![](https://i2.wp.com/cdn-images-1.medium.com/max/1024/1*TInM9cvQxK1jLHwS6xJkVg.jpeg?w=578&ssl=1)
Puzzles
Author: ExcelBI
All files (xlsx with puzzle and R with solution) for each and every puzzle are available on my Github. Enjoy.
Puzzle #173
![](https://i2.wp.com/cdn-images-1.medium.com/max/1024/1*WLF9FFhoK_UUxL_GKsQufQ.jpeg?w=578&ssl=1)
One of contestants said: Why should we break such a nice table? Because indeed it is what we have to do: break table into cross-tab which is cross-tab only from appearance. We need to generate some empty cells to achieve it, and then get some summaries from the data
Loading libraries and data
library(tidyverse) library(readxl) library(glue) input = read_excel("Power Query/PQ_Challenge_173.xlsx", range = "A1:B731") test = read_excel("Power Query/PQ_Challenge_173.xlsx", range = "D1:H27")
Transformation
result1 = input %>% mutate(quarter = quarter(Date), year = year(Date), month = month(Date, label = TRUE, locale = "en"), month_num = month(Date)) %>% summarise(`Total Sale` = sum(Sale), .by = c("year", "quarter", "month", "month_num")) %>% mutate(years_row = row_number(), sales_perc = `Total Sale` / sum(`Total Sale`), .by = "year") %>% mutate(quarter_row = row_number(), .by = c("year","quarter")) %>% mutate(display_year = ifelse(years_row == 1, year, NA_character_), display_quarter = ifelse(quarter_row == 1, quarter, NA_integer_)) %>% select(year, Year = display_year, Quarter = display_quarter, Month = month, month_num, `Total Sale`, `Sale %` = sales_perc) totals = result1 %>% summarise(`Total Sale` = sum(`Total Sale`), `Sale %` = sum(`Sale %`), .by = "year") %>% mutate(Year = glue("{year} Total") %>% as.character(), Quarter = NA_integer_, Month = NA_character_, month_num = NA_integer_) %>% select(year, Year, Quarter, Month, `Total Sale`, `Sale %`) result = bind_rows(result1, totals) %>% arrange(year, month_num) %>% select(-c(year, month_num))
Validation
all.equal(result, test, check.attributes = FALSE) # [1] TRUE
Puzzle #174
![](https://i0.wp.com/cdn-images-1.medium.com/max/1024/1*XxvzR3K_7hf7rdjKqXRPHQ.jpeg?w=578&ssl=1)
Power Query challenges are always about solving problems, usually even real-life ones. Today we have people that sold something, but somebody just written down how much they get for full period of engagement. But we need data in month granulation. And one more note, we need to differentiate months by number of days. We need to calculate monthly sales and running sum resetting itself every year.
Loading libraries and data
library(tidyverse) library(readxl) library(padr) input = read_excel("Power Query/PQ_Challenge_174.xlsx", range = "A1:D5") test = read_excel("Power Query/PQ_Challenge_174.xlsx", range = "F1:J20")
Transformation
result = input %>% pivot_longer(cols = -c(1, 4), names_to = "date", values_to = "value") %>% select(-date) %>% group_by(Emp) %>% pad() %>% fill(Sales, .direction = "down") %>% mutate(days = n(), daily_sales = Sales / days, month = floor_date(value, "month"), year = year(value)) %>% ungroup() %>% summarise(`Monthly Sales` = sum(daily_sales), `From Date` = min(value), `To Date` = max(value), .by = c("Emp", "month", "year")) %>% mutate(`Running Total` = cumsum(`Monthly Sales`), .by = c("Emp", "year")) %>% select(Emp, `From Date`, `To Date`, `Monthly Sales`, `Running Total`) %>% mutate(across(c(4:5), ~round(., digits = 2)))
Validation
# not all results match because of floating point precision # structure achieved
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.
Continue reading: PowerQuery Puzzle solved with R
Analysis of Key Points
The text mainly revolves around solving two puzzles using the R programming language, with particular focus on data manipulation, loading libraries, and data transformations.
Puzzle #173
The objective of Puzzle #173 is to break down a table into a cross-tab, generate empty cells, and produce summaries from the data. This puzzle demonstrated the use of several libraries, particularly the tidyverse, readxl, and glue.
Key Steps:
- Data is loaded from Power Query using the read_excel function.
- Transformations are carried out to mutate (modify) the data and summarise total sales.
- The ‘totals’ are determined by summarising the total sales and sale percentages on a yearly basis.
- The final result is obtained by binding ‘result1’ and ‘totals’ using bind_rows, followed by a selection of specific columns.
Puzzle #174
In Puzzle #174, the focus is on calculating the monthly sales and running totals, while resetting the sum every year. This puzzle employs the use of tidyverse, readxl, and padr libraries.
Key Steps:
- Data is loaded from Power Query using the read_excel function.
- Transformations are carried out using pivot_longer function and padding.
- Date manipulation and intermediate computations are performed (such as computing daily_sales).
- Monthly Sales and Running Total are calculated. There is a note that not all results match due to floating point precision.
Long-Term Implications and Possible Future Developments
The author’s walk-through of the puzzles illustrates the robust functionality offered by R programming in data manipulation tasks, such as breaking tables into cross-tabs and calculating sums by different periods. This has long-term implications for data analysis, particularly in complex environments requiring efficient computations and transformations on large sets of data. As the technology evolves, R programming is expected to introduce new and more efficient libraries for data manipulation.
Actionable Advice
Practitioners investing in R will need to stay updated with the developments in the R packages ecosystem. Additionally, understanding the nuances of mathematical calculations in R (e.g., floating point precision) can help prevent surprises and inaccuracies in calculations. To better grasp the power and flexibility of R in data manipulation, practicing real-life challenges as outlined in the puzzles can be extremely beneficial.