I am wanting to join a dataframe using a function within 'by' of the left_join in R.
Savings_YC <- Savings_YC %>%
left_join(WAP_FULL, by = c("Client", "CompanyCode", "Material", "STOCK_UOM"))
What I want is to join Savings_YC to WAP_FULL on LEFT(Savings_YC$CompanyCode, 4) = WAP_FULL$CompanyCode. As an example this is what it would be in SQL:
SELECT * FROM Savings_YC syc
LEFT JOIN WAP_FULL w ON syc.Client = w.Client and LEFT(syc.CompanyCode, 4) = w.CompanyCode AND syc.Material = w.Material ...
I am assuming I will want to use substr(x, 1, 4) for the LEFT function, but how do I include that within the left_join function? I have tried the following and getting an error.
Savings_YC <- Savings_YC %>% # Not sure this will work
left_join(WAP_FULL, by = c("Client", substr("Savings_YC.CompanyCode",1,4) = "WAP_FULL.CompanyCode", "Material", "STOCK_UOM"))