Skip to content

graph_from_incidence_matrix() shouldn't implicitly count NA elements as edges #918

@davidskalinder

Description

@davidskalinder

Describe the bug

At the moment, when graph_from_incidence_matrix() is run with the default options multiple = FALSE and weighted = NULL, an edge is created for matrix elements with NA.

The documentation says this:

graph_from_incidence_matrix() can operate in two modes, depending on the multiple argument. If it is FALSE then a single edge is created for every non-zero element in the incidence matrix.

I suppose it might be argued that NA elements are non-zero and so the function is working as expected (and that therefore this is a feature request). However that argument runs contrary to the usual understanding that NA should not be treated as non-zero (since a missing value might in fact be zero). At a minimum, the current behavior is surprising in many contexts.

To reproduce

library(igraph)
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union

m <- matrix(c(0, 1, 1, 0, 1, 1), 2, 3)
n <- replace(m, 3, NA)

m
#>      [,1] [,2] [,3]
#> [1,]    0    1    1
#> [2,]    1    0    1
n
#>      [,1] [,2] [,3]
#> [1,]    0   NA    1
#> [2,]    1    0    1
graph_from_incidence_matrix(m)
#> IGRAPH 0e356cc U--B 5 4 -- 
#> + attr: type (v/l)
#> + edges from 0e356cc:
#> [1] 1--4 1--5 2--3 2--5
graph_from_incidence_matrix(n)
#> IGRAPH 82e1755 U--B 5 4 -- 
#> + attr: type (v/l)
#> + edges from 82e1755:
#> [1] 1--4 1--5 2--3 2--5

Created on 2023-10-25 with reprex v2.0.2

Version information

  • R/igraph version: 1.5.1
  • R version: 4.1.2
  • Operating system: Ubuntu 20.04.6 LTS
> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.6 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3

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       

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

other attached packages:
[1] igraph_1.5.1

loaded via a namespace (and not attached):
 [1] viridis_0.6.4      pkgload_1.3.3      tidyr_1.3.0        tidygraph_1.2.3   
 [5] viridisLite_0.4.2  R.utils_2.12.2     ggraph_2.1.0       shiny_1.7.5.1     
 [9] yaml_2.3.7         remotes_2.4.2.1    ggrepel_0.9.4      sessioninfo_1.2.2 
[13] pillar_1.9.0       glue_1.6.2         digest_0.6.33      promises_1.2.1    
[17] polyclip_1.10-6    colorspace_2.1-0   R.oo_1.25.0        htmltools_0.5.6.1 
[21] httpuv_1.6.11      clipr_0.8.0        pkgconfig_2.0.3    devtools_2.4.5    
[25] haven_2.5.3        unix_1.5.5         purrr_1.0.2        xtable_1.8-4      
[29] scales_1.2.1       processx_3.8.2     tweenr_2.0.2       later_1.3.1       
[33] ggforce_0.4.1      tibble_3.2.1       styler_1.10.2      generics_0.1.3    
[37] farver_2.1.1       ggplot2_3.4.4      usethis_2.2.2      ellipsis_0.3.2    
[41] cachem_1.0.8       withr_2.5.1        cli_3.6.1          magrittr_2.0.3    
[45] crayon_1.5.2       mime_0.12          memoise_2.0.1      evaluate_0.22     
[49] ps_1.7.5           R.methodsS3_1.8.2  fs_1.6.3           fansi_1.0.5       
[53] R.cache_0.16.0     MASS_7.3-55        forcats_1.0.0      pkgbuild_1.4.2    
[57] profvis_0.3.8      tools_4.1.2        prettyunits_1.2.0  hms_1.1.3         
[61] lifecycle_1.0.3    stringr_1.5.0      munsell_0.5.0      reprex_2.0.2      
[65] callr_3.7.3        compiler_4.1.2     rlang_1.1.1        grid_4.1.2        
[69] rstudioapi_0.15.0  htmlwidgets_1.6.2  miniUI_0.1.1.1     corrr_0.4.4       
[73] rmarkdown_2.25     gtable_0.3.4       graphlayouts_1.0.1 R6_2.5.1          
[77] gridExtra_2.3      knitr_1.44         dplyr_1.1.3        fastmap_1.1.1     
[81] utf8_1.2.3         stringi_1.7.12     Rcpp_1.0.11        vctrs_0.6.4       
[85] tidyselect_1.2.0   xfun_0.40          urlchecker_1.0.1 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions