0

Suppose I have two data frame like these:

df1 <- data.frame(
customer = c('john','sally','bill','david','sam','jake'),
coupon_id = c('a',NA,'d',NA,NA,'c'),
final_price = c(100,50,40,25,100,200))

df2 <- data.frame(
coupon_id = c('a','b','c','d'),
prct_off = c(.15,.2,.25,.50))

Goal: conditionally test each row of df1 to see if it has a coupon. If it does, multiply use the percent off it has in the df2 data frame to reduce the final price by the specified amount. If it doesn't leave the final price as is.

I think I'm close, but I'm failing to do things row wise. I'm doing something like,

df1$final_price <- ifelse(df1$coupon_id %in% df2$coupon_id, df1$final_price == df1$final_price*(1-df2$prct_off),df1$final_price)

but this doesn't work correctly.

Help?

Saul Feliz
  • 668
  • 3
  • 11
  • 20
  • 1
    This would be easier if you just `merge()` or `dplyr::left_join()` to combine your data first, then do the calculation. – MrFlick Jan 15 '18 at 19:39
  • Basically exactly the same problem as the question i've marked as duplicate. – MrFlick Jan 15 '18 at 19:40

0 Answers0