1

I am trying to figure the mode of a data set, while displaying "NONE" if there is no mode. I am currently using Gregor's function as commented below.

examples:

{1,1,2,2,3} Expected results 1 2(success)

{NA,NA,NA,1,1,1,3,3} Expected results NA 1(success)

{1,2,3,4,5} Expected result NONE (success)

{1,1,1,1,1} Expected result 1 (fail)

EDIT: I am trying out with if...and functions, as I realize there seems to be some logical problems. But I can't seem to crack it

smode<-function(x,...) {
ux <- unique(x)
tx <- tabulate(match(x, ux))
if(((unique(x)>=2)&(length(unique(tx)) == 1)) {

    return("NONE")
}
max_tx <- tx == max(tx)
return(ux[max_tx])
}

Right now, {1,1,1,1,1,1} or any other alike just displays NONE Just checked again, and {1,1,1,2,2,2} displays NONE too instead of 1 2

I'm assuming it will be the same for more similiar things.

EDIT: I DID IT

function(x,...) {
ux <- unique(x)
tx <- tabulate(match(x, ux))
if(sum(tx)/length(tx) == 1) {

    return("NONE")
}
max_tx <- tx == max(tx)
return(ux[max_tx])
}
Jerry Lim
  • 13
  • 5
  • What have you tried? Maybe `sort()` the table and use an `if()` statement to check that the maximum value is unique...? – Gregor Thomas Jan 15 '18 at 19:11
  • You seem possibly hung up on the case where all values occur the same number of times, but what output would you want for, e.g., `a = c(1, 1, 2, 2, 3)`? There isn't a unique mode because `1` and `2` are tied, but `3` only occurs once. Would you want `1, 2` or `"NONE"` as a result? – Gregor Thomas Jan 15 '18 at 19:18
  • Possible duplicate of [Is there a built-in function for finding the mode?](https://stackoverflow.com/questions/2547402/is-there-a-built-in-function-for-finding-the-mode) – Imran Ali Jan 15 '18 at 19:20
  • @Gregor , I would want 1,2 then. None only occurs when all of them occurred equally. – Jerry Lim Jan 15 '18 at 19:24
  • Well, in that case I would suggest you check the number of unique values in `smode`. The functions `length()` and `unique()` should help you out. I bet you can figure it out from there. – Gregor Thomas Jan 15 '18 at 19:32
  • Your edits are nice to see, but you removed all the description of your problem. It would still be nice to know what you are trying to do. – Gregor Thomas Jan 17 '18 at 18:19

1 Answers1

1

How about this:

Mode <- function(x) {
  ux <- unique(x)
  tx <- tabulate(match(x, ux))
  if(length(unique(tx)) == 1) {
    message("None")
    return(NA)
  }
  max_tx <- tx == max(tx)
  return(ux[max_tx])
}

Mode(1:5)
# None
# [1] NA
Mode(c(1, 1, 2, 3))
# [1] 1
Mode(c(1, 1, 2, 2, 3))
# [1] 1 2
Mode(c(1, 1, 2, 2, 3, 3))
# None
# [1] NA

It's a slight modification of Ken William's Mode function in this answer.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Many thanks! What is the logic behind `if(length(unique(tx)) == 1)` though? – Jerry Lim Jan 17 '18 at 18:33
  • Think about it. Try some examples. I'm sure you can work it out. My `tx` is equivalent to `smode` in your original attempt. – Gregor Thomas Jan 17 '18 at 18:38
  • Is the result suppose to show "None" above the df and not actually "None" in the cell? – Kristine Jun 11 '19 at 22:01
  • @Kristine Generally, I think functions should be type-consistent in that if they usually return a number then they should always return a number (or at least something of class `numeric`). Since `"NONE"` can't be numeric I had the function print `"None"` as a message and return an `NA` value if there is no mode. – Gregor Thomas Jun 11 '19 at 22:09
  • Oh yes! I missed that. Thank you. – Kristine Jun 11 '19 at 22:21