原始数据:
adj_mat <- matrix(
c(NA, 100, 2, 1, 100,
4, NA, 100, 100, 3,
100, 3, NA, 2, 4,
100, 1, 5, NA, 100,
1, 100, 4, 100, NA
),
nrow = 5, ncol = 5, byrow = TRUE
)
adj_mat
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] NA 100 2 1 100
#> [2,] 4 NA 100 100 3
#> [3,] 100 3 NA 2 4
#> [4,] 100 1 5 NA 100
#> [5,] 1 100 4 100 NA
1) 将行索引、列索引和邻接矩阵的值组合成 3 个矩阵的列表:
rows_cols_vals_matrices <- list(row_indices = row(adj_mat),
col_indices = col(adj_mat),
values = adj_mat)
rows_cols_vals_matrices
#> $row_indices
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 1 1 1 1 1
#> [2,] 2 2 2 2 2
#> [3,] 3 3 3 3 3
#> [4,] 4 4 4 4 4
#> [5,] 5 5 5 5 5
#>
#> $col_indices
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 1 2 3 4 5
#> [2,] 1 2 3 4 5
#> [3,] 1 2 3 4 5
#> [4,] 1 2 3 4 5
#> [5,] 1 2 3 4 5
#>
#> $values
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] NA 100 2 1 100
#> [2,] 4 NA 100 100 3
#> [3,] 100 3 NA 2 4
#> [4,] 100 1 5 NA 100
#> [5,] 1 100 4 100 NA
2)展平矩阵:
vectorized_matrices <- lapply(rows_cols_vals_matrices, as.vector)
vectorized_matrices
#> $row_indices
#> [1] 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
#>
#> $col_indices
#> [1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5
#>
#> $values
#> [1] NA 4 100 100 1 100 NA 3 1 100 2 100 NA 5 4 1 100
#> [18] 2 NA 100 100 3 4 100 NA
3) 将向量绑定到一个 3 列矩阵中:
melted <- do.call(cbind, vectorized_matrices)
head(melted)
#> row_indices col_indices values
#> [1,] 1 1 NA
#> [2,] 2 1 4
#> [3,] 3 1 100
#> [4,] 4 1 100
#> [5,] 5 1 1
#> [6,] 1 2 100
4) 删除第 3 列为 NA 的行:
filtered <- melted[!is.na(melted[, 3]), ]
filtered
#> row_indices col_indices values
#> [1,] 2 1 4
#> [2,] 3 1 100
#> [3,] 4 1 100
#> [4,] 5 1 1
#> [5,] 1 2 100
#> [6,] 3 2 3
#> [7,] 4 2 1
#> [8,] 5 2 100
#> [9,] 1 3 2
#> [10,] 2 3 100
#> [11,] 4 3 5
#> [12,] 5 3 4
#> [13,] 1 4 1
#> [14,] 2 4 100
#> [15,] 3 4 2
#> [16,] 5 4 100
#> [17,] 1 5 100
#> [18,] 2 5 3
#> [19,] 3 5 4
#> [20,] 4 5 100
5)将其全部包装成一个函数:
as_edgelist.adj_mat <- function(x, .missing = NA) {
# if there arerow/colnames or non-numeric data, you'll need to to use a data frame to
# handle heterogenous data types
stopifnot(is.numeric(x) & is.null(dimnames(x)))
melted <- do.call(cbind, lapply(list(row(x), col(x), x), as.vector))
if (is.na(.missing)) {
out <- melted[!is.na(melted[, 3]), ]
} else {
out <- melted[melted[, 3] != .missing, ]
}
out
}
6) 试一试:
as_edgelist.adj_mat(adj_mat)
#> [,1] [,2] [,3]
#> [1,] 2 1 4
#> [2,] 3 1 100
#> [3,] 4 1 100
#> [4,] 5 1 1
#> [5,] 1 2 100
#> [6,] 3 2 3
#> [7,] 4 2 1
#> [8,] 5 2 100
#> [9,] 1 3 2
#> [10,] 2 3 100
#> [11,] 4 3 5
#> [12,] 5 3 4
#> [13,] 1 4 1
#> [14,] 2 4 100
#> [15,] 3 4 2
#> [16,] 5 4 100
#> [17,] 1 5 100
#> [18,] 2 5 3
#> [19,] 3 5 4
#> [20,] 4 5 100