rdataframeloopsgroup

modify/create column based on groups defined externally


I have a predefined list containing a list of items as below:

group_1<-c("A","B","C")
group_2<-c("D","E","F")

item<-c("A","B","C","D","E","F")
item_price<-(1:6)

df<-data.frame(item,item_price)

> df
  item item_price
1    A          1
2    B          2
3    C          3
4    D          4
5    E          5
6    F          6

And now, what I would like to do is to create a new column, multiplying item_price by an arbitrary number, in this case, for items in group_1, item_price will be multiplied by 0.1 and 0.2 for those in group 2. Desired output as below:

> df
  item item_price df_new_rate
1    A          1         0.1
2    B          2         0.2
3    C          3         0.3
4    D          4         0.8
5    E          5         1.0
6    F          6         1.2

Since I will need to loop thru item column and see which group different item belongs to, do I need to write a for loop here? Or if there is a short and clear cut way to do so? All ideas are appreciated.


Solution

  • You can use ifelse or case_when if you have more than 2 groups:

    library(dplyr)
    
    df %>% 
      mutate(df_new_rate = case_when(item %in% group_1 ~ 0.1,
                                     item %in% group_2 ~ 0.2,
                                     .default = 0) * item_price)
    #>   item item_price df_new_rate
    #> 1    A          1         0.1
    #> 2    B          2         0.2
    #> 3    C          3         0.3
    #> 4    D          4         0.8
    #> 5    E          5         1.0
    #> 6    F          6         1.2