在 R 中按行将二进制矩阵转换为列索引列表

Convert binary matrix to column-indexed lists by row in R

给定一个二进制矩阵B,我通常可以使用Blist <- apply(B==1, 1, which)逐行获取1的列索引列表。但是,这在例如 rowSums(B)[1] == rowSums(B)[2].

时不起作用

这是一个有效的例子:

> B <- matrix(c(1,1,0,0,1,0,0,0,1),3,3)
> B
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    1    1    0
[3,]    0    0    1
> Blist <- apply(B==1, 1, which)
> Blist
[[1]]
[1] 1

[[2]]
[1] 1 2

[[3]]
[1] 3

这是一个不起作用的例子:

> B <- matrix(c(1,0,0,0,1,0,0,0,1),3,3)
> B
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    1    0
[3,]    0    0    1
> Blist <- apply(B==1, 1, which)
> Blist
[1] 1 2 3

在这种情况下,期望的结果是:

> Blist
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

如何修改 Blist <- apply(B==1, 1, which) 以便始终将结果组织为列表?

simplify个参数,默认为TRUE。来自 ?apply`

的默认用法

apply(X, MARGIN, FUN, ..., simplify = TRUE)

simplify - a logical indicating whether results should be simplified if possible.

此外,它指出

If the calls to FUN return vectors of different lengths, or if simplify is FALSE, apply returns a list of length prod(dim(X)[MARGIN]) with dim set to MARGIN if this has length greater than one.

apply(B==1, 1, which, simplify = FALSE)
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3
m = which(B == 1, arr.ind=TRUE)
split(m[,"col"], m[,"row"])
# $`1`
# [1] 1

# $`2`
# [1] 2

# $`3`
# [1] 3