从列表创建稀疏矩阵

时间:2020-10-13 20:35:57

标签: r sparse-matrix

我希望从sparse matrix创建一个list。我可以使用此处介绍的方法来创建典型的matrix

Class of output object differs as input data differs

下面是典型的matrix的一个小的可复制示例。

set.seed(1234)

# determine number of observations in each sample
n.samples <- 20
max.obs   <- 10
obs.per.sample <- sample(0:max.obs, size = n.samples, prob = c(0.70,0.15,0.05,0.03,rep(0.01,7)), replace = TRUE)

# determine size of each observation in a sample
# here obs.size is a list
my.sizes   <- seq(10, 32, 2)
size.probs <- c(0.02,0.04,0.06,0.08,0.10,0.12,0.14,0.16,0.14,0.08,0.04,0.02)
obs.size   <- sapply(obs.per.sample, function(x) sample(my.sizes, size = x, prob = size.probs, replace=TRUE))

# create matrix of observation sizes in all samples
max.samples <- max(lengths(obs.size))
mat <- matrix(c(sapply(obs.size, `[`, 1:max.samples)), nrow = n.samples, byrow = TRUE)
mat[is.na(mat)] <- 0
mat
#      [,1] [,2] [,3]
# [1,]    0    0    0
# [2,]    0    0    0
# [3,]    0    0    0
# [4,]    0    0    0
# [5,]   22   22    0
# [6,]    0    0    0
# [7,]    0    0    0
# [8,]    0    0    0
# [9,]    0    0    0
#[10,]    0    0    0
#[11,]    0    0    0
#[12,]    0    0    0
#[13,]    0    0    0
#[14,]   24   24   26
#[15,]    0    0    0
#[16,]   16    0    0
#[17,]    0    0    0
#[18,]    0    0    0
#[19,]    0    0    0
#[20,]    0    0    0

2 个答案:

答案 0 :(得分:1)

也许您可以尝试以下基本R选项

l <- lengths(obs.size)
mat <- matrix(0,length(obs.size),max(l))
mat[cbind(rep(which(l>0),l[l>0]),sequence(l[l>0]))] <- unlist(obs.size)

其中非零值的索引以cbind(rep(which(l>0),l[l>0]),sequence(l[l>0]))为特征,而您只需将非零值unlist(obs.size)分配给这些位置。

  • 输出
> mat
      [,1] [,2] [,3]
 [1,]    0    0    0
 [2,]    0    0    0
 [3,]    0    0    0
 [4,]    0    0    0
 [5,]   22   22    0
 [6,]    0    0    0
 [7,]    0    0    0
 [8,]    0    0    0
 [9,]    0    0    0
[10,]    0    0    0
[11,]    0    0    0
[12,]    0    0    0
[13,]    0    0    0
[14,]   24   24   26
[15,]    0    0    0
[16,]   16    0    0
[17,]    0    0    0
[18,]    0    0    0
[19,]    0    0    0
[20,]    0    0    0

如果您需要稀疏矩阵,则可能需要Matrix软件包的帮助,例如

library(Matrix)

l <- lengths(obs.size)
mat <- sparseMatrix(
  i = rep(which(l > 0), l[l > 0]),
  j = sequence(l[l > 0]),
  x = unlist(obs.size)
)

这样

> mat
16 x 3 sparse Matrix of class "dgCMatrix"

 [1,]  .  .  .
 [2,]  .  .  .
 [3,]  .  .  .
 [4,]  .  .  .
 [5,] 22 22  .
 [6,]  .  .  .
 [7,]  .  .  .
 [8,]  .  .  .
 [9,]  .  .  .
[10,]  .  .  .
[11,]  .  .  .
[12,]  .  .  .
[13,]  .  .  .
[14,] 24 24 26
[15,]  .  .  .
[16,] 16  .  .

答案 1 :(得分:1)

我想obs.size是您的清单。用稀疏矩阵表示sparseMatrix包中的Matrix。您需要提供i,j索引以及非零条目的值。

对于我来说,这是因为行索引:

nonzero = sapply(obs.size,length)
i = rep(1:length(obs.size),nonzero)
i
[1]  5  5 14 14 14 16

j是列索引,我的大脑现在无法正常工作,因此下面的代码可能会辅助:

j = unlist(tapply(i,i,seq_along))

然后创建矩阵:

library(Matrix)
sparseMatrix(i=i,j=j,x=unlist(obs.size))
16 x 3 sparse Matrix of class "dgCMatrix"
              
 [1,]  .  .  .
 [2,]  .  .  .
 [3,]  .  .  .
 [4,]  .  .  .
 [5,] 22 22  .
 [6,]  .  .  .
 [7,]  .  .  .
 [8,]  .  .  .
 [9,]  .  .  .
[10,]  .  .  .
[11,]  .  .  .
[12,]  .  .  .
[13,]  .  .  .
[14,] 24 24 26
[15,]  .  .  .
[16,] 16  .  .
相关问题