IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    PowerQuery Puzzle solved with R

    Numbers around us发表于 2024-10-08 11:28:59
    love 0
    [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.

    #223–224

    Puzzles

    Author: ExcelBI

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

    Puzzle #223

    As usual on weekends we are mainly doing table transformations. Sometimes it need simple manouvers and sometimes very complicated almost magical tricks. And today we have exactly that situation. First one is like: squeeze, pull, push, twist. Simple usage of basic functions.

    If you are curious, why I illustrated it with scene of cafeteria… We sometimes need to complete tables like lunch on tray. 😀

    Loading libraries and data

    library(tidyverse)
    library(readxl)
    
    path = "Power Query/PQ_Challenge_223.xlsx"
    input = read_excel(path, range = "A1:D14")
    test  = read_excel(path, range = "F1:J8")

    Transformation

    result = input %>%
      unite("Code", c("Type", "Code"), sep = "") %>%
      mutate(col = ifelse(row_number() %% 2 == 0, 2, 1), 
             row = (row_number() + 1) %/% 2,
             .by = Group) %>%
      pivot_wider(names_from = col, values_from = c(Code, Value), names_sep = "") %>%
      select(-row)

    Validation

    all.equal(result, test)
    #> [1] TRUE

    Puzzle #224

    And the second one is one of the most complicated to solve because it is totally untidy. We have different data in the same row, we have headers every couple of rows. Fortunatelly I found a way to rebuild it in less than 15 LOC.

    Loading libraries and data

    library(tidyverse)
    library(readxl)
    library(janitor)
    
    path = "Power Query/PQ_Challenge_224.xlsx"
    input = read_excel(path, range = "A1:D12")
    test  = read_excel(path, range = "F1:I20")

    Transformation

    result = input %>%
      mutate(date = ifelse(str_detect(Column1, "\\d"), Column1, NA)) %>%
      fill(date) %>%
      set_names(.[1, ]) %>%
      rename("Name" = 1, "date" = 5) %>%
      filter(!str_detect(Name, "\\d")) %>%
      mutate(date = coalesce(excel_numeric_to_date(as.numeric(date)), mdy(date))) %>%
      pivot_longer(-c(date, Name), names_to = "Data", values_to = "Value") %>%
      na.omit() %>%
      select(Date = date, Name, Data, Value) %>%
      mutate(Value = as.numeric(Value), 
             Date = as.POSIXct(Date))

    Validation

    all.equal(result, test, check.attributes = FALSE)
    #> [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


沪ICP备19023445号-2号
友情链接