Is there a way to reorder rows and columns of a matrix to create a tight angle in R?

I have a large matrix that contains 1.2 and is missing (encoded as NA). A matrix has 500,000 rows per 10,000 columns. Approximately 0.05% of 1- or 2-values, and the remaining values ​​are equal to NA.

I would like to reorder the rows and columns of the matrix so that the upper left corner of the matrix contains a relatively large number of 1s and 2s compared to the rest of the matrix. In other words, I would like to create a relatively datarich subset of the matrix, by reordering the rows and columns of the matrix.

Is there an efficient way to achieve this in R, possibly using a library? I would also be interested in Python or Java solutions, but I would prefer to do this in R, as this is the most familiar language to me.

I thought that perhaps there are a number of optimization procedures that I could use as my working matrix, too large to make a reorganization by eye.

I returned a set of corrections so that the question remains consistent with the current answers.

0
source share
2 answers

Like this?

#some sparse data set.seed(42) p <- 0.0005 mat <- matrix(sample(c(1, 2, NA), 1e4, TRUE, c(p/2, p/2, 1-p)), ncol=50) #order columns and rows by the number of NA values in them mat <- mat[order(rowSums(is.na(mat))), order(colSums(is.na(mat)))] #only show columns and rows containing non-NA values mat[rowSums(!is.na(mat)) > 0, colSums(!is.na(mat)) > 0] # [,1] [,2] [,3] [,4] [,5] [,6] # [1,] NA NA NA NA 2 NA # [2,] NA NA NA NA NA 2 # [3,] NA NA 2 NA NA NA # [4,] NA 1 NA NA NA NA # [5,] 1 NA NA NA NA NA # [6,] NA NA NA 2 NA NA 
+3
source

Something like that?

 Rgames> bar [,1] [,2] [,3] [,4] [,5] [1,] NA NA NA NA NA [2,] 1 NA NA NA 3 [3,] NA NA NA NA NA [4,] 2 NA NA NA 4 [5,] NA NA NA NA NA Rgames> rab<-bar[order(bar[,1]),] Rgames> rab [,1] [,2] [,3] [,4] [,5] [1,] 1 NA NA NA 3 [2,] 2 NA NA NA 4 [3,] NA NA NA NA NA [4,] NA NA NA NA NA [5,] NA NA NA NA NA Rgames> rab[,order(rab[1,])] [,1] [,2] [,3] [,4] [,5] [1,] 1 3 NA NA NA [2,] 2 4 NA NA NA [3,] NA NA NA NA NA [4,] NA NA NA NA NA [5,] NA NA NA NA NA 

CHANGE, as Roland noted, in a more general situation that will not come close. Now, if allowed to "mix" rows and columns, this would do this:

 for(j in 1:nrow(mfoo)) mat[j,]<-mat[j,order(mat[j,])] for(j in 1:ncol(mat)) mat[,j]<-mat[order(mat[,j]),j] 

I suspect that it’s not what I need, so I’ll leave and think about the order of the “criteria”

+2
source

Source: https://habr.com/ru/post/988185/


All Articles