0

How can I get a output data frame with most repeated value in each row for its corresponding row ?

df <- data.frame(x=seq(1:10), y=seq(1:10), z=sample(1:10), w=sample(1:10), v=sample(1:10))

I tried apply but it didn't output the correct answer : apply(df, 1, frequency)

solved by following :

getmode <- function(v) {
   uniqv <- unique(v)
   uniqv[which.max(tabulate(match(v, uniqv)))]
}

Then

apply(df, 1, getmode)

Seymoo
  • 177
  • 2
  • 15
  • 2
    Try `Mode <- function(x) { + ux <- unique(x) + ux[which.max(tabulate(match(x, ux)))] + }; > apply(df, 1, Mode)` – akrun Feb 13 '18 at 10:51
  • 1
    `apply(df, 1, some_function)` and you can find a bunch of `some_function` [here](https://stackoverflow.com/questions/2547402/is-there-a-built-in-function-for-finding-the-mode) – Sotos Feb 13 '18 at 10:51

0 Answers0