【发布时间】:2016-12-14 13:56:46
【问题描述】:
我正在尝试将邻接矩阵处理为社区列表, 输出应该是一个向量列表(字符类型)
例如
l1[1] = c("a","b","c")
l1[2] = c("d")
l1[3] = c("e","f")
我正在尝试在 for 循环中实现这一点:
l1=vector("list")
for(kn in 1:nrow(adjFinal)){
temp = c()
for(tm in 1:ncol(adjFinal))
{
if(adjFinal[kn,tm]==1)
temp=c(temp,colnames(adjFinal)[tm])
}
l1[kn] = temp
}
但我不断收到“要替换的项目数不是替换长度的倍数”的警告,以及长度为 1 的 3 个向量的列表 喜欢:
l1[1] = "a"
l1[2] = "d"
l1[3] = "e"
更新: 这是adjFinal:
3 x 6 sparse Matrix of class "dgCMatrix"
a b c d e f
b 1 1 1 . . .
d . . . 1 . .
e . . . . 1 1
和输入(adjFinal):
new("dgCMatrix"
, i = c(0L, 0L, 0L, 1L, 2L, 2L)
, p = 0:6
, Dim = c(3L, 6L)
, Dimnames = list(c("b", "d", "e"), c("a", "b", "c", "d", "e", "f"))
, x = c(1, 1, 1, 1, 1, 1)
)
提前谢谢你。
【问题讨论】:
-
请提供
dput(adjFinal)的输出以使您的示例可重现。 -
你能分享
dput和预期的输出吗? -
apply(adjFinal,1,function(x) colnames(adjFinal)[which(x==1)])适合你吗? -
请更正您的
dput -
@Nicola ,是的......它产生了我想要的东西。请提交您的评论作为答案,以便我接受。