How to process a matrix (data frame) automatically in R

I have a 41x41 similarity matrix (or data frame), as below (I am attaching the full version in the application):

     V1    V2      V3      V4      V5      V6
 V1   1    0.068   0.211   0.285   0.198   0.047
 V2  0.068   1     0.851   0.450   0.277   0.014
 V3  0.211  0.851     1    0.660   0.420   0.113
 V4  0.285  0.450  0.660      1    0.896   0.466
 V5  0.198  0.277  0.420    0.896    1     0.241
 V6  0.047  0.014  0.113    0.466   0.241     1

I want to create a recommended list for each vector (Vx), most similar to the first place, different from the last. Therefore, I think that I need to extract two columns (or two rows), sort the values ​​by decreasing, and extract the column names. However, when I try to select columns automatically, it does not work.

ms<-readLines("E:/exp/ccsm.txt", encoding = "UTF-8")
d = as.data.frame(ms)
for(dcol in 2:length(ms))
 {
   temp<-d[,c(1,dcol)]
   nlist<-temp[order(d[,dcol], decreasing=T)]
   lname<-nlist[,1]
 }
Show Traceback
Rerun with Debug
Error in `[.data.frame`(d, , c(1, dcol)) : undefined columns selected

, /? . - , . , . , . .

+4
2

, , data.frame.

ccsm.txt pwd R :

ms <- readLines('ccsm.txt',encoding='UTF-8');
ms;
## [1] "     V1    V2      V3      V4      V5      V6"
## [2] " V1   1    0.068   0.211   0.285   0.198   0.047"
## [3] " V2  0.068   1     0.851   0.450   0.277   0.014"
## [4] " V3  0.211  0.851     1    0.660   0.420   0.113"
## [5] " V4  0.285  0.450  0.660      1    0.896   0.466"
## [6] " V5  0.198  0.277  0.420    0.896    1     0.241"
## [7] " V6  0.047  0.014  0.113    0.466   0.241     1"
d <- as.data.frame(ms);
d;
##                                                 ms
## 1         V1    V2      V3      V4      V5      V6
## 2  V1   1    0.068   0.211   0.285   0.198   0.047
## 3  V2  0.068   1     0.851   0.450   0.277   0.014
## 4  V3  0.211  0.851     1    0.660   0.420   0.113
## 5  V4  0.285  0.450  0.660      1    0.896   0.466
## 6  V5  0.198  0.277  0.420    0.896    1     0.241
## 7   V6  0.047  0.014  0.113    0.466   0.241     1
names(d);
## [1] "ms"
dim(d);
## [1] 7 1
sapply(d,class);
##       ms
## "factor"

, readLines()/as.data.frame() 7--1 data.frame, .

read.table() ( , fread() data.table):

d <- read.table('ccsm.txt');
d;
##       V1    V2    V3    V4    V5    V6
## V1 1.000 0.068 0.211 0.285 0.198 0.047
## V2 0.068 1.000 0.851 0.450 0.277 0.014
## V3 0.211 0.851 1.000 0.660 0.420 0.113
## V4 0.285 0.450 0.660 1.000 0.896 0.466
## V5 0.198 0.277 0.420 0.896 1.000 0.241
## V6 0.047 0.014 0.113 0.466 0.241 1.000
names(d);
## [1] "V1" "V2" "V3" "V4" "V5" "V6"
dim(d);
## [1] 6 6
sapply(d,class);
##        V1        V2        V3        V4        V5        V6
## "numeric" "numeric" "numeric" "numeric" "numeric" "numeric"

, :

for (dcol in 2:length(ms)) {
    temp <- d[,c(1,dcol)];
    nlist <- temp[order(d[,dcol],decreasing=T)];
    lname <- nlist[,1];
};
## Error in `[.data.frame`(temp, order(d[, dcol], decreasing = T)) :
##   undefined columns selected

. -, ms , 7, . 6 . , d[,c(1,dcol)] , dcol 7.

dcol 7, temp[order(d[,dcol],decreasing=T)] . , temp - data.frame, d. temp, , , . order() 1 6, d ( 6 d), 3 6 - , temp data.frame.

:

apply(d,1,order,decreasing=T);
##      V1 V2 V3 V4 V5 V6
## [1,]  1  2  3  4  5  6
## [2,]  4  3  2  5  4  4
## [3,]  3  4  4  3  3  5
## [4,]  5  5  5  6  2  3
## [5,]  2  1  1  2  6  1
## [6,]  6  6  6  1  1  2

, , :

apply(d,1,function(x) names(d)[order(x,decreasing=T)]);
##      V1   V2   V3   V4   V5   V6
## [1,] "V1" "V2" "V3" "V4" "V5" "V6"
## [2,] "V4" "V3" "V2" "V5" "V4" "V4"
## [3,] "V3" "V4" "V4" "V3" "V3" "V5"
## [4,] "V5" "V5" "V5" "V6" "V2" "V3"
## [5,] "V2" "V1" "V1" "V2" "V6" "V1"
## [6,] "V6" "V6" "V6" "V1" "V1" "V2"

, "" , ...[-1,].

+1

:

diag(mat) <- -99
mat2 <- t(apply(mat,2,function(x) rev(order(x))))[,-ncol(mat)]
#> mat2
#   [,1] [,2] [,3] [,4] [,5]
#V1    4    3    5    2    6
#V2    3    4    5    1    6
#V3    2    4    5    1    6
#V4    5    3    6    2    1
#V5    4    3    2    6    1
#V6    4    5    3    1    2

, , , . sort() rev(), . . -99, , , . . ( ). .

< >

mat <- as.matrix(read.table(text="V1    V2      V3      V4      V5      V6
 V1   1    0.068   0.211   0.285   0.198   0.047
 V2  0.068   1     0.851   0.450   0.277   0.014
 V3  0.211  0.851     1    0.660   0.420   0.113
 V4  0.285  0.450  0.660      1    0.896   0.466
 V5  0.198  0.277  0.420    0.896    1     0.241
 V6  0.047  0.014  0.113    0.466   0.241     1", header=T))
+1

All Articles