1

I have a problem that I want to solve through R but I can't find out how to do it or even whether it is actually possible.

My goal is to add a column to a 'Target' table through a 'Mapping' table that holds conditional formulas that should get evaluated to TRUE against the values in some of the columns of the 'Target' table.

The point is that I want that the user can update the formulas in the 'Mapping' table or even add some formulas without having to update the R code. So formulas need to get evaluated dynamically.

Let's see a concrete sample below to clarify this. I have a 'Target' table as below:

Product    Customer  Quantity   Sales   Margin
Product 1   Customer 1  1000    15000   4.6%
Product 1   Customer 2  2000    30000   7.5%
Product 2   Customer 1  3000    60000   10%
Product 2   Customer 3  1500    30000   12%
Product 2   Customer 4  2000    40000   8%

I have a 'Mapping' table as below:

Condition                                           Method
Sales > 50000                                       Method 1
Sales < 50000 AND Margin < 5%                       Method 2
Sales < 50000 AND (Margin >= 5% AND Margin < 10%)   Method 3
Sales < 50000 AND Margin >= 10%                     Method 4

I assume that conditions are consistent and only one formula can evaluate to true for any combination of values in 'Target' table.

And I want to get the following result with the 'Method' column added to the 'Target' table :

Product     Customer    Quantity    Sales   Margin  Method
Product 1   Customer 1  1000    15000   4.6%    Method 2
Product 1   Customer 2  2000    30000   7.5%    Method 3
Product 2   Customer 1  3000    60000   10%     Method 1
Product 2   Customer 3  1500    30000   12%     Method 4
Product 2   Customer 4  2000    40000   8%      Method 3

My initial idea is to cross join the two tables and then to evaluate the formula in the 'Condition' table against the columns in the 'Target' table. Then I could only keep the rows where the condition evaluates to TRUE.

But I can't find out how to evaluate the 'Condition' formula dynamically against the other columns in the cross table.

I would then appreciate any idea/comment about this issue!

Any solution that would also include another way to express the 'Condition' formula would also be highly appreciated!

Thanks

1

1 Answer 1

0

Using case_when from the dplyr package would allow you to forgo using the "Mapping" dataframe. You can invoke the conditions directly:

library(dplyr)

Product <- c(1,1,2,2,2)
Customer <- c(1,2,1,3,4)
Quantity <- c(1000,2000,3000,1500,2000)
Sales <- c(15000,30000, 60000,30000,40000)
# For now, it's easier to work without percentage signs in "Margin"
Margin <- c(0.046, 0.075, 0.1, 0.12, 0.08) 

target_df <- data.frame(Product,Customer,Quantity,Sales,Margin)

target_df <- mutate(target_df,
                    Method = case_when(Sales > 50000 ~ "Method 1",
                                       Sales < 50000 &  Margin < 0.05 ~ "Method 2",
                                       Sales < 50000 & Margin >= 0.05 & Margin < 0.10 ~ "Method 3",
                                       Sales < 50000 & Margin >= 0.01 ~ "Method 4" ),
                    Margin = sprintf("%1.2f%%", 100*Margin) # We can re-add percentage signs here
                    )

Output:

  Product Customer Quantity Sales Margin   Method
1       1        1     1000 15000  4.60% Method 2
2       1        2     2000 30000  7.50% Method 3
3       2        1     3000 60000 10.00% Method 1
4       2        3     1500 30000 12.00% Method 4
5       2        4     2000 40000  8.00% Method 3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.