I'm analyzing donor data from an appeal. Let's say we have this data frame, which has an ID, the amount given, and the ask amounts from an appeal card:

dfgive <- data.frame(id = c(1, 2, 3, 4, 5),
   gift = c(20, 1000, 1500, 300, 500),
   ask1 = c(50, 5000, 5000, 100, 2500),
   ask2 = c(100, 3750, 3750, 250, 5000),
   ask3 = c(250, 2500, 2500, 500, 10000),
   gift_diff_1 = c(-30, -4000, -3500, 200, -2000),
   gift_diff_2 = c(-80, -2750, -2250, 50, -4500),
   gift_diff_3 = c(-230, -1500, -1000, -200, -9500),
   mindiff = c(-230, -4000, -3500, -200, -9500))

The gift difference from each ask amount and the minimum difference were created with the code below.

mutate(gift_diff_1 = gift - ask1) %>%
   mutate(gift_diff_2 = gift - ask2) %>%
   mutate(gift_diff_3 = gift - ask3) %>%
   mutate(mindiff=min(gift_diff_1, gift_diff_2, gift_diff_3))

What I'm hoping to do next is create a new column called gift_closest that would identify which of the ask amounts (ask1, ask2, or ask3) is closest in absolute value to the gift. We could use either the derived gift_diff columns or do it wholly within the equation. I can't get my head around how to best do it.

The value of the new column would be one of "ask1", "ask2", or "ask3". Ideally in a dplyr chain as that's where I'm creating the entire data set which will include a bunch of demographic fields.

thanks,

Got a quick response on stackoverflow which answered the question perfectly...so for reference...

dfgive %>%
  mutate(abs = pmin(abs(gift - ask1), abs(gift - ask2), abs(gift - ask3)),
         gift_close = case_when(abs(gift - ask1) == abs ~ "ask1",
                                abs(gift - ask2) == abs ~ "ask2",
                                abs(gift - ask3) == abs ~ "ask3",
                                TRUE ~ NA_character_))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.