Sort_matrix_elements: Sort rows and columns of a matrix according to a criterion

Description Usage Arguments Author(s) See Also Examples

Description

sort_descOnDiag() where possible, sort rows and columns of a matrix to put the highest values on diagonal in a descending order. The highest value is paced at the top left corner.

sort_colSums() sorts columns of a matrix according to the values of column sums.
sort_colMax() sorts columns of a matrix according to the maximum value in each column.

sort_rowSums() sorts rows of a matrix according to the values of row sums.
sort_rowMax() sorts rows of a matrix according to the maximum value in each row.

Possible application: in a cluster analysis sort rows and columns of a cross tablation by the best match (illustration in section "Expamples").

Usage

1
2
3
4
5
6
7
8
9
sort_descOnDiag(M, importance = which.min(dim(M)), na.rm = TRUE)

sort_colSums(M, decreasing = TRUE, na.rm = TRUE)

sort_rowSums(M, decreasing = TRUE, na.rm = TRUE)

sort_rowMax(M, decreasing = TRUE, na.rm = TRUE)

sort_colMax(M, decreasing = TRUE, na.rm = TRUE)

Arguments

M

A matrix.

importance

Numecic value (either 1 for rows or 2 for columns) which is used to resolve ties when 2 equally important matches are found (i.e., when absolute values are equal). If importance = 1, the element's importance is calculated by row-wise proportions, if importance = 2 - by column-wise proportions. Element with higher proportional value is selected as having higher priority.

na.rm

Logical. Should missing values (including NaN) be omitted from the calculations?

decreasing

Logical. Should the sort be decreasing?

Author(s)

Vilmantas Gegzna

See Also

order, sort.

Other matrix operations in spMisc: corr_vec2mat(), indMatrix(), nTri2nDiag(), revalueMatrix(), which.in(), which.max.all(), which.max.perRow()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
library(spMisc)

# Create matrix
M0 <- matrix(c(0, 11,8, 0,
               12,0, 0, 0,
               0, 0, 0, 9,
               0, 0, 9, 0,
               0, 0, 1, 0),byrow=TRUE,nc=4)

# Give names for rows and columns
M0 <- pkgmaker::addnames(M0)

# Sort and print
M0
sort_descOnDiag(M0)
sort_colSums(M0)
sort_rowSums(M0)
sort_colMax(M0)
sort_rowMax(M0)

# Application in cluster analysis ==========================================
set.seed(1)
Clusters <- kmeans(iris[,-5], 3)$cluster
Species <- iris$Species

# Regular cross tabulation
TABLE <- table(Species, Clusters)
TABLE

##                Clusters
##  Species       1  2  3
##  setosa       50  0  0
##  versicolor    0  2 48
##  virginica     0 36 14

# Arranged by the best match
TABLE_best_match <- sort_descOnDiag(TABLE)
TABLE_best_match

##              Clusters
##  Species       1  3  2
##  setosa       50  0  0
##  versicolor    0 48  2
##  virginica     0 14 36

#------------------------------------------------------------------
# Parameter `importance` for proportional importance:

 #>
Matrix <- matrix(c(3,0,0,2,3,0,0,0,5),3,3)
Matrix <- pkgmaker::addnames(Matrix)
 #>          col1 col2 col3
 #>   row1    3    2    0
 #>   row2    0    3    0
 #>   row3    0    0    5

# Row-wise importance
Matrix_by_row <- sort_descOnDiag(Matrix, importance = 1)
Matrix_by_row
 #>          col3 col2 col1
 #>   row3    5    0    0
 #>   row2    0    3    0
 #>   row1    0    2    3  <---- 2 is in row 3

# Column-wise importance
Matrix_by_col <- sort_descOnDiag(Matrix, importance = 2)
Matrix_by_col
 #>          col3 col1 col2
 #>   row3    5    0    0
 #>   row1    0    3    2  <---- 2 is in row 2
 #>   row2    0    0    3

GegznaV/spMisc documentation built on April 26, 2020, 5:59 p.m.