简体   繁体   中英

Dplyr Left Join on Case When

Using two tables is it feasible to left join the right table on certain conditions?

In this case, if right table, ProductHierarchyType == "LINE" then I would want to join conditionally on the column name ProductLineID in the left table. This would continue on based on the Hierarchy of CLASS>GROUP>SUBGROUP>LINE.

I attempted to create additional columns, with the prodmap2 but this gave me additional columns that I'm not confident on how to get that condition right.

prodmap2<-prodmap%>% mutate(ProdClass = case_when(ProductHierarchyType=="CLASS" ~ ProductHierarchyID))%>% mutate(ProdGroup = case_when(ProductHierarchyType=="GROUP" ~ ProductHierarchyID))%>% mutate(ProdSUBGroup = case_when(ProductHierarchyType=="SUBGROUP" ~ ProductHierarchyID))%>% mutate(ProdLine = case_when(ProductHierarchyType=="LINE" ~ ProductHierarchyID))

Left table:

structure(list(TerritoryKey = c("800046", "800046", "800046", 
"800046", "800046", "800046"), Material = c("000-40", "003-01", 
"003-40", "004-00", "004-05", "005-40"), TotalSales = c(61.68, 
94.27, 48227.14, 422.88, 45.4, 3723.92), ProductClassID = c("0012", 
"0012", "0012", "0012", "0012", "0012"), ProductGroupID = c("00120001", 
"00120001", "00120001", "00120002", "00120002", "00120001"), 
    ProductSubGroupID = c("001200010002", "001200010002", "001200010002", 
    "001200020002", "001200020002", "001200010002"), ProductLineID = c("001200010002000001", 
    "001200010002000001", "001200010002000001", "001200020002000001", 
    "001200020002000001", "001200010002000001"), StartDate = c("1/1/2016", 
    "1/1/2016", "1/1/2016", "1/1/2016", "1/1/2016", "1/1/2016"
    ), EndDate = c("12/31/2099", "12/31/2099", "12/31/2099", 
    "12/31/2099", "12/31/2099", "12/31/2099")), .Names = c("TerritoryKey", 
"Material", "TotalSales", "ProductClassID", "ProductGroupID", 
"ProductSubGroupID", "ProductLineID", "StartDate", "EndDate"), row.names = c(NA, 
-6L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), vars = "TerritoryKey", drop = TRUE, indices = list(
    0:5), group_sizes = 6L, biggest_group_size = 6L, labels = structure(list(
    TerritoryKey = "800046"), row.names = c(NA, -1L), class = "data.frame", vars = "TerritoryKey", drop = TRUE, .Names = "TerritoryKey"))

Right Table:

structure(list(CompProfileID = c("ALTC", "ALTC", "ALTC", "ALTC", 
"ALTC", "ALTC"), ProductBucketID = c("CORE", "CORE", "CORE", 
"CORE", "CORE", "CORE"), ProductHierarchyID = c("001200010001000001", 
"001200010001000003", "001200010001000009", "001200010002", "001200010003000001", 
"001200010004000004"), ProductHierarchyType = c("LINE", "LINE", 
"LINE", "SUBGROUP", "LINE", "LINE"), ExclusionFlag = c("N", "N", 
"N", "N", "N", "N"), StartDate = c("2017-01-01", "2017-01-01", 
"2017-01-01", "2017-01-01", "2017-01-01", "2017-01-01"), EndDate = c("2099-12-31", 
"2099-12-31", "2099-12-31", "2099-12-31", "2099-12-31", "2099-12-31"
), ExclusionType = c("", "", "", "", "", "")), .Names = c("CompProfileID", 
"ProductBucketID", "ProductHierarchyID", "ProductHierarchyType", 
"ExclusionFlag", "StartDate", "EndDate", "ExclusionType"), row.names = c(NA, 
6L), class = "data.frame")

Conditionally joining on multiple columns is hard. I recommend to transform the data into "tidy data" form before joining. I mean, collapsing columns related to ID into one pair columns of key and value.

library(tidyr)
library(dplyr, warn.conflicts = FALSE)

left_table_tidy <- left_table %>%
  ungroup() %>% 
  tibble::rowid_to_column(var = "unique_ID") %>% 
  gather(key = "ID_type", value = "ID", matches("Product.*ID")) %>%
  mutate(ID_type = recode(ID_type,
                          ProductClassID = "CLASS",
                          ProductGroupID = "GROUP",
                          ProductSubGroupID = "SUBGROUP",
                          ProductLineID = "LINE"))

Then, you can join the data by the types of ID and the IDs.

table_joined <- inner_join(left_table_tidy,
                           right_table,
                           by = c("ID_type" = "ProductHierarchyType",
                                  "ID"      = "ProductHierarchyID"))

As you notice, this join may hit multiple types per original row. So you need to sort rows in the order of "CLASS>GROUP>SUBGROUP>LINE" and pick the first in order to remove duplication.

table_joined %>%
  group_by(unique_ID) %>%
  arrange(factor(ID_type, levels = c("CLASS", "GROUP", "SUBGROUP", "LINE"))) %>%
  slice(1L)
#> # A tibble: 4 x 14
#> # Groups:   unique_ID [4]
#>   unique_ID TerritoryKey Material TotalSales StartDate.x  EndDate.x
#>       <int>        <chr>    <chr>      <dbl>       <chr>      <chr>
#> 1         1       800046   000-40      61.68    1/1/2016 12/31/2099
#> 2         2       800046   003-01      94.27    1/1/2016 12/31/2099
#> 3         3       800046   003-40   48227.14    1/1/2016 12/31/2099
#> 4         6       800046   005-40    3723.92    1/1/2016 12/31/2099
#> # ... with 8 more variables: ID_type <chr>, ID <chr>, CompProfileID <chr>,
#> #   ProductBucketID <chr>, ExclusionFlag <chr>, StartDate.y <chr>,
#> #   EndDate.y <chr>, ExclusionType <chr>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM