The code below is generating the mode value considering the columns Method1
, Method2
, Method3
and Method4
. However, notice that for alternative 10
and 12
it has the same mode value, that is, it has a value of 2
. However, I would like my Mode
column to have different values, as if it were a rank. Therefore, the alternative that had Mode=1
is the best, but I have no way of knowing the second best alternative, because it has two numbers 2
in the Mode
column. Do you have suggestions on what approach I can take?
database<-structure(list(Alternatives = c(3, 4, 5, 6, 7, 8, 9, 10, 11, 12),
Method1 = c(1L, 10L, 7L, 8L, 9L, 6L, 5L, 3L, 4L, 2L), Method2 = c(1L,
8L, 6L, 7L, 10L, 9L, 4L, 2L, 3L, 5L), Method3 = c(1L,
10L, 7L, 8L, 9L, 6L, 4L, 2L, 3L, 5L), Method4 = c(1L,
9L, 6L, 7L, 10L, 8L, 5L, 3L, 4L, 2L)), class = "data.frame", row.names = c(NA,
10L))
ModeFunc <- function(Vec) {
tmp <- sort(table(Vec),decreasing = TRUE)
Nms <- names(tmp)
if(max(tmp) > 1) {
as.numeric(Nms[1])
} else NA}
output <- database |> rowwise() |>
mutate(Mode = ModeFunc(c_across(Method1:Method4))) %>%
data.frame()
> output
Alternatives Method1 Method2 Method3 Method4 Mode
1 3 1 1 1 1 1
2 4 10 8 10 9 10
3 5 7 6 7 6 6
4 6 8 7 8 7 7
5 7 9 10 9 10 9
6 8 6 9 6 8 6
7 9 5 4 4 5 4
8 10 3 2 2 3 2
9 11 4 3 3 4 3
10 12 2 5 5 2 2
@prubin answer
output |> mutate(Weight = Mode +
1/ nrow(database) * ((Method1 + Method2 + Method3 + Method4) / 4)) |>
mutate(rank = rank(Weight))
Alternatives Method1 Method2 Method3 Method4 Mode Weight rank
1 3 1 1 1 1 1 1.100 1
2 4 10 8 10 9 10 10.925 10
3 5 7 6 7 6 6 6.650 6
4 6 8 7 8 7 7 7.750 8
5 7 9 10 9 10 9 9.950 9
6 8 6 9 6 8 6 6.725 7
7 9 5 4 4 5 4 4.450 5
8 10 3 2 2 3 2 2.250 2
9 11 4 3 3 4 3 3.350 4
10 12 2 5 5 2 2 2.350 3