Search code examples
rdata-cleaning

Create a new column based on formula involving other column


First, I want to create a new column called "total price" based on the type of product defined by column "type"(1 : 5). Data looks like this:

df <- data.frame('type'=c(1,2,3,4,5), 'quantity'=c(1,20,3,4,50))
df
  type quantity
1   1    1         
2   2   20
3   3    3
4   4    4
5   5   50

Total price can be calculated by multiplying the quantity of each product by the corresponding price:

***The price for each type of product: Type 1 - $10; Type 2- $20;Type 3- $30;Type 4- $40;Type 5- $50

So that I would expect something looks like this:

       type quantity totalprice
    1   1    1           10
    2   2   20          400
    3   3    3           90
    4   4    4          160
    5   5   50         2500

Then, I need to join this newly created column with the original data as the last column.

I guess the pseudo code looks something like this:

Part 1)
if df$type == "1" , totalprice = df$quantity * 10,
if df$type == "2", totalprice = df$quantity * 20,
if df$type == "3", totalprice = df$quantity * 30,
if df$type == "4", totalprice = df$quantity * 40

Part2)
Collect these values in a column called totalprice and join it to df.

Thank you very much for your help.


Solution

  • I agree with @Elin, create a reference dataframe with all the types and their corresponding price. merge it with your original df by "type" and then multiply quantity * price

    ref_data <- data.frame(type = 1:5, price = c(10, 20, 30, 40, 50))
    transform(merge(df, ref_data, by = "type"), totalprice = quantity * price)
    
    #  type quantity price totalprice
    #1    1        1    10         10
    #2    2       20    20        400
    #3    3        3    30         90
    #4    4        4    40        160
    #5    5       50    50       2500
    

    In dplyr, you could do

    library(dplyr)
    inner_join(df, ref_data, by = "type") %>%
        mutate(totalprice = quantity * price)