The code below checks the correlation of three methods (TOPSIS, PROMETHEE and VIKOR
) with a specific method (DISTANCE
), and the one with the highest correlation is chosen. In this case below, it is TOPSIS
.
library(dplyr)
library(tidyr)
df<-structure(list(Alternatives = c(3, 4, 5, 6, 7, 8, 9, 10, 11, 12),
DISTANCE = c(1L, 10L, 7L, 8L, 9L, 6L, 5L, 3L, 4L, 2L), TOPSIS = c(1L,
8L, 7L, 6L, 10L, 9L, 4L, 3L, 2L, 5L), PROMETHEE = c(1L, 10L,
7L, 8L, 9L, 6L, 4L, 2L, 3L, 5L), VIKOR = c(1, 9, 7, 6, 10,
8, 5, 4, 3, 2)), class = "data.frame", row.names = c(NA,
-10L))
> df
Alternatives DISTANCE TOPSIS PROMETHEE VIKOR
1 3 1 1 1 1
2 4 10 8 10 9
3 5 7 7 7 7
4 6 8 6 8 6
5 7 9 10 9 10
6 8 6 9 6 8
7 9 5 4 4 5
8 10 3 3 2 4
9 11 4 2 3 3
10 12 2 5 5 2
output<-df %>%
summarise(across(TOPSIS:VIKOR, ~cor.test(., DISTANCE,
method = "spearman")$estimate)) %>%
pivot_longer(cols = everything()) %>%
slice_head(n=1) %>%
slice_max(value) %>%
pull(name) %>%
select(df, .)
TOPSIS
1 1
2 8
3 7
4 6
5 10
6 9
7 4
8 3
9 2
10 5
Now, I'd like to consider my column Alternatives
from my df
dataframe along with the result of output
, and generate a variable k
, which is the number of alternatives when the method value is 1. Example:
Alternatives TOPSIS
1 3 1
2 4 8
3 5 7
4 6 6
5 7 10
6 8 9
7 9 4
8 10 3
9 11 2
10 12 5
So the value of k
would be equal to 3, because 1 is in the row of Alternatives = 3
.
CodePudding user response:
You can use the following code:
output<-df %>%
summarise(across(TOPSIS:VIKOR,
~cor.test(., DISTANCE,method = "spearman")$estimate)) %>%
pivot_longer(cols = everything()) %>%
slice_head(n=1) %>%
slice_max(value) %>%
pull(name) %>%
all_of(.) %>%
select(df, Alternatives, . ) %>%
filter (.[[2]] == 1)
The above code will return a tibble:
## Alternatives TOPSIS
##1 3 1
You can extract then extract the value from the column Alternatives if needed.