有没有一种方法可以重新排序矩阵的行和列,以在R中创建一个密集的角? [英] Is there a way to reorder the rows and columns of matrix to create a dense corner, in R?

查看:52
本文介绍了有没有一种方法可以重新排序矩阵的行和列,以在R中创建一个密集的角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大矩阵,其中包含1,2,并且缺少(编码为NA)值.矩阵具有500000行乘10000列. 1或2值大约占0.05%,其余值为NA.

我想对矩阵的行和列进行重新排序,以便与矩阵的其余部分相比,矩阵的左上角包含相对较高的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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆