0

I'm struggling with the code below. My aim is to rewrite a price with a Mode only if only one price differs from the rest. I know that with n()==1 we can choose unique prices without duplicates, but how to add that only if there is one such price. Tried with count(), but got an error.

      library("dplyr")
      
      Mode <- function(x) {
      ux <- unique(x)
      ux[which.max(tabulate(match(x, ux)))]
      }
      pp <- pp %>% 
      group_by(PPCODE) %>% 
      mutate(PRICE = ifelse(count(n() == 1)==1, Mode(PRICE), PRICE), .keep="unused")

Output of dput:

structure(list(OUTLETID = c("11N", "12B", "17C", 
"44Oo", "1NN", "5CC", "AA1", "11A"), PPCODE = c(4623, 
4623, 4623, 4111, 4111, 4623, 4111, 4111), 
PRICE = c(1.45, 1.45, 1.45, 5.11, 5.11, 1.42, 5.13, 4.5))), 
row.names = c(NA, -4L), class = c("tbl_df", "tbl", "data.frame"))

So, in the first ppcode 4623 price should be changed as only one price differs (1.42 should be changed to 1.45), but with the second ppcode they should stay as they are (because 2 prices differ 4.5 and 5.13 from mode).

Bambeil
  • 309
  • 1
  • 8
  • 3
    Please share a [reproductible example](https://stackoverflow.com/help/minimal-reproducible-example) of your data using `dput(pp)` please. – MonJeanJean Oct 21 '21 at 05:44
  • Share mock data that shows what you need to do. We don't care about your actual data – Sotos Oct 21 '21 at 05:46
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Oct 21 '21 at 05:47
  • 1
    @MonJeanJean updated – Bambeil Oct 21 '21 at 06:01

1 Answers1

1

You may count how many values differ from mode in each group using sum(PRICE != Mode(PRICE)) and apply the Mode function if it is less than equal to 1.

library(dplyr)

pp %>% 
  group_by(PPCODE) %>%
  mutate(PRICE = if(sum(PRICE != Mode(PRICE)) <= 1) Mode(PRICE) else PRICE) %>%
  ungroup

#  OUTLETID PPCODE PRICE
#  <chr>     <dbl> <dbl>
#1 11N        4623  1.45
#2 12B        4623  1.45
#3 17C        4623  1.45
#4 44Oo       4111  5.11
#5 1NN        4111  5.11
#6 5CC        4623  1.45
#7 AA1        4111  5.13
#8 11A        4111  4.5 
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Close, but not exactly. I want to apply mode if more than one unique price but ONLY IF ONE PRICE DIFFERS. So, as written in my question with ppcode 4111 they shouldn't be changed as 2 prices differ from mode, only with 4623 this rule should be applied (because there one price differ). – Bambeil Oct 21 '21 at 06:24
  • @Bambeil Can you try the updated answer ? – Ronak Shah Oct 21 '21 at 06:36