Skip to content

graph_from_adjacency_matrix() with mode="upper" errors due to NA values in the lower-triangular part #2218

@LTLA

Description

@LTLA

What happens, and what did you expect instead?

Pretty much as it says on the tin. Using the latest igraph (2.2.0) on R 4.5.0:

x <- matrix(runif(100), ncol=10, nrow=10)
x[lower.tri(x)] <- NA
igraph::graph_from_adjacency_matrix(x, mode="upper", weighted=TRUE, diag=FALSE)
## Error in `igraph::graph_from_adjacency_matrix()`:
## ! Cannot create a graph object because the adjacency matrix contains NAs.
## Run `rlang::last_trace()` to see where the error occurred.

This error seems unnecessary given that we have explicitly instructed the function to only use the upper triangular part of the matrix. The contents of the lower triangular part should not be of any concern to graph_from_adjacency_matrix(). Ideally, the NA values (or NaN, or Inf, or whatever weirdness that might not make sense as edge weights) should be ignored.

You might ask why we have NAs in our matrix in the first place. In my packages, some of the matrices that I generate are explicitly upper-triangular. To ensure that there is no ambiguity in their interpretation, I fill the lower triangular part with NAs so that people don't try to use it. See OSCA-source/OSCA.advanced#23 for an example of a use case broken by the new behavior. (bluster builds are similarly broken on the Bioconductor build system.)

To reproduce

x <- matrix(runif(100), ncol=10, nrow=10)
x[lower.tri(x)] <- NA
igraph::graph_from_adjacency_matrix(x, mode='upper', weighted=TRUE, diag=FALSE)
#> Error in `igraph::graph_from_adjacency_matrix()`:
#> ! Cannot create a graph object because the adjacency matrix contains
#>   NAs.

Created on 2025-10-22 with reprex v2.1.1

System information

R version 4.5.0 Patched (2025-04-24 r88177)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 22.04.5 LTS

Matrix products: default
BLAS:   /home/luna/Software/R/R-4-5-branch/lib/libRblas.so 
LAPACK: /home/luna/Software/R/R-4-5-branch/lib/libRlapack.so;  LAPACK version 3.12.1

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: America/Los_Angeles
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] igraph_2.2.0

loaded via a namespace (and not attached):
[1] compiler_4.5.0  magrittr_2.0.4  cli_3.6.5       lifecycle_1.0.4
[5] pkgconfig_2.0.3 rlang_1.1.6    

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions