0

I'm doing a voting function for my predictions and I arranged the multi-class reasons into a dataframe(similar to below). However, I can't do a majority Vote on it for every row, answer is NA.

Tried to use Apply(), majorityvote()

t<-c(3,4,5,6,7,4,4,5,4)
y<-c(3,4,5,6,4,4,4,4,4)
z<-c(3,4,5,6,7,4,4,5,4)
o<-data.frame(t,y,z)

Mode <- function(x) {
ux <- unique(x)
ux[names(which.max(table(x)))]
}
apply(o, 1, Mode)
  • 1
    Your `Mode` function is bad. It can only be used on a named vector, and the way you apply it the input is unnamed. See the R-FAQ on [calculating the mode](https://stackoverflow.com/a/8189441/903061). Use one of those functions instead. – Gregor Thomas Mar 26 '19 at 20:56
  • I'd recommend closing as a duplicate of the R-FAQ linked in my above comment. – Gregor Thomas Mar 27 '19 at 03:37

1 Answers1

0

One way to do this is as below:

#setting up model function
mode <- function(x){ 
  ta = table(x)
  tam = max(ta)
  mod = as.numeric(names(ta)[ta == tam])
  mod = mod[1] 
  return(mod)
}

t <-c(3,4,5,6,7,4,4,5,4)
y <-c(3,4,5,6,4,4,4,4,4)
z <-c(3,4,5,6,7,4,4,5,4)
o <-data.frame(t,y,z)


apply(o, 1, mode)
Majid
  • 1,836
  • 9
  • 19