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