Home > Enterprise >  Insert conditions when I have the same values for the mode or when I don't have the mode value
Insert conditions when I have the same values for the mode or when I don't have the mode value

Time:07-04

The code below generates the mode value from the values obtained by Methods 1, 2, 3 and 4. But notice that in some cases I have correct mode values, for example, alternatives 3 and 4, but incorrect ones, such as in alternative 5, as it has two values of 7 and two values of 6, but the mode value is showing 6. Furthermore, in alternatives 11 and 12, it has no a mode value, because it has different values for both methods. So for these incorrect cases, that is, when I have 2 equal values for the same alternative and when I have no mode value, I would like to consider the value obtained by Method 1 to be the mode value. I inserted below the correct output.

Executable code below:

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, 5L, 3L), Method3 = c(1L, 
10L, 7L, 8L, 9L, 6L, 4L, 2L, 3L, 5L), Method4 = c(1L, 
9L, 6L, 7L, 10L, 8L, 5L, 3L, 2L, 4L)), 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       5       3       2   NA
10           12       2       3       5       4   NA

The correct output would then be:

Alternatives Method1 Method2 Method3 Method4 Mode
       3       1       1       1       1      1
       4      10       8      10       9      10
       5       7       6       7       6      7
       6       8       7       8       7      8
       7       9      10       9      10      9
       8       6       9       6       8      6
       9       5       4       4       5      5
      10       3       2       2       3      3
      11       4       5       3       2      4
      12       2       3       5       4      2

CodePudding user response:

You could use some conventional mode() function,

mode <- function(x) {
  ux <- unique(x)
  tb <- tabulate(match(x, ux))
  ux[tb == max(tb)]
}

and update values using ifelse in mapply.

mds <- apply(database[-1], 1, mode) |> setNames(database$Alternatives)
mapply(\(x, y) ifelse(length(x) > 1, y, x), mds, database$Method1)
# 3  4  5  6  7  8  9 10 11 12 
# 1 10  7  8  9  6  5  3  4  2 

So, altogether it could look like this:

database |>
  cbind(Mode=mapply(\(x, y) ifelse(length(x) > 1, y, x), 
                    apply(database[-1], 1, mode), 
                    database$Method1))
#    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    7
# 4             6       8       7       8       7    8
# 5             7       9      10       9      10    9
# 6             8       6       9       6       8    6
# 7             9       5       4       4       5    5
# 8            10       3       2       2       3    3
# 9            11       4       5       3       2    4
# 10           12       2       3       5       4    2
  •  Tags:  
  • r
  • Related