0
我试图处理邻接矩阵到社区的列表, 输出应该是向量(类型的字符)- [R创建字符的列表矢量
例如列表
l1[1] = c("a","b","c")
l1[2] = c("d")
l1[3] = c("e","f")
我试图在实现这一目标的循环:
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
和dput(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)'的输出以使您的示例具有可再现性。 – nicola
请问您可以分享'dput'和预期的输出吗? – Sotos
是否适用(adjFinal,1,function(x)colnames(adjFinal)[which(x == 1)])'为你工作? – nicola