0

我正在尝试将边缘列表转换为矩阵。以下代码将完美地适用于具有多行的边缘列表。

# 0) Load the data
my_data_org <- readr::read_csv(paste(input_path, file_name, sep = ""), col_names=FALSE)
# 1) Replace NA with 0
my_data_new[is.na(my_data_org)] <- 0
# 2) converting to adjacency matrix 
tmp <- apply(my_data_new[-1], 1, function(x) x[x != 0]) # from each row, get non-zero values
tmp <- do.call(rbind, lapply(tmp, function(x) t(combn(x, m = 2)))) # from the output of the above, find all 
my_graph <- graph_from_edgelist(tmp, directed = FALSE)
adj_mat <- as_adjacency_matrix(my_graph)
write.matrix(adj_mat, file=paste(output_path, 'output_', file_name_new), ",")

但是,如果数据只有一行,我会在下面收到此错误:

Error in combn(x, m = 2) : n < m 

我试图用 0 值填充一行,但仍然得到相同的错误。

我该如何处理这种边缘情况?

4

0 回答 0