Using summary
, here is an example:
mat <- Matrix(data = c(1, 0, 2, 0, 0, 3, 4, 0, 0), nrow = 3, ncol = 3,
dimnames = list(Origin = c("A", "B", "C"),
Destination = c("X", "Y", "Z")),
sparse = TRUE)
mat
# 3 x 3 sparse Matrix of class "dgCMatrix"
# Destination
# X Y Z
# A 1 . 4
# B . . .
# C 2 3 .
summ <- summary(mat)
summ
# 3 x 3 sparse Matrix of class "dgCMatrix", with 4 entries
# i j x
# 1 1 1 1
# 2 3 1 2
# 3 3 2 3
# 4 1 3 4
data.frame(Origin = rownames(mat)[summ$i],
Destination = colnames(mat)[summ$j],
Weight = summ$x)
# Origin Destination Weight
# 1 A X 1
# 2 C X 2
# 3 C Y 3
# 4 A Z 4
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…