有没有一种方法可以重新排序矩阵的行和列,以在R中创建一个密集的角? [英] Is there a way to reorder the rows and columns of matrix to create a dense corner, in R?
问题描述
我想对矩阵的行和列进行重新排序,以便与矩阵的其余部分相比,矩阵的左上角包含相对较高的1s和2s.换句话说,我想通过对矩阵的行和列进行重新排序来创建一个相对数据丰富的矩阵子集.
是否有可能使用库在R中实现此目的的有效方法?我也会对Python或Java解决方案感兴趣,但我更喜欢在R中执行此操作,因为它是我最熟悉的语言.
我认为可能有一些优化程序可供我使用,因为我的工作矩阵太大,无法通过肉眼进行重组.
我已还原了一组编辑,以便使该问题与当前答案保持一致.
喜欢吗?
#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
I have a large matrix which comprises 1,2 and missing (coded as NA) values. The matrix has 500000 rows by 10000 columns. There are approximately 0.05% 1- or 2-values, and the remaining values are NA.
I would like to reorder the rows and columns of the matrix so that the top left corner of the matrix comprises a relatively high 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 matrix rows and columns.
Is there an efficient method of achieving this in R, perhaps using a library? I would also be interested in solutions in Python or Java, but I would prefer to perform this in R as it is the language that's most familiar to me.
I thought that there maybe a set of optimisation procedures that I could use as my working matrix is too large to do the reorganisation by eye.
I have reverted a set of edits so that the question remains consistent with the current 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
这篇关于有没有一种方法可以重新排序矩阵的行和列,以在R中创建一个密集的角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!