Search code examples
rfunctionmode

Getting the first three values of a dataset's mode


I am trying to learn about mode with R. I some code online, which I modified a little:

getmode <- function(v) {
  uniqv <- unique(v)
  first <- uniqv[which.max(tabulate(match(v, uniqv)))]
  mode <- first
  # cancel "first" occurrences
  second<- uniqv[which.max(tabulate(match(v, uniqv)))]
  mode <- second
  # cancel "second" occurrences
  third <- uniqv[which.max(tabulate(match(v, uniqv)))]
  # cancel "third" occurrences
  mode <- third
  print(mode)
  }

It returns only the first value (the max one), but I would like to print the first three ones. My idea was to get cancel all the occorrences of the first most frequent value (#), re-use the previous code and get the second most frequent value, cancel it and repeat again the routine to get the third. It seems easy, but I can't get it to work. Can anybody help me?

Otherwise, do you know any other way to get the first 3 max values of each attribute of my dataset?

Summary() works, but does not focus on the mode nor only on some of my attributes (it gives valuse for all attributes instead of the ones I want).


Solution

  • An approach similar to what you were thinking.

    Using the mode function from here.

    Mode <- function(x) {
      ux <- unique(x)
      ux[which.max(tabulate(match(x, ux)))]
    }
    

    We can call Mode function three times after removing the previous mode value.

    x <- rep(1:5, c(6, 5, 1, 2, 9))
    three_modes <- numeric(3)
    
    for(i in seq_along(three_modes)) {
      three_modes[i] <- Mode(x)
       x <- x[x!=three_modes[i]]
    }
    
    three_modes
    #[1] 5 1 2
    

    You can replace 3 with whichever number you like to get top n modes.