using graph.adjacency() in R

前端 未结 2 1704
时光说笑
时光说笑 2020-12-16 20:32

I have a sample code in R as follows:

library(igraph)
rm(list=ls())
dat=read.csv(file.choose(),header=TRUE,row.names=1,check.names=T) # read .csv file
m=as.m         


        
相关标签:
2条回答
  • 2020-12-16 21:10

    Just a small working example below, much clearer than CSV input.

    library('igraph');
    adjm1<-matrix(sample(0:1,100,replace=TRUE,prob=c(0.9,01)),nc=10); 
    g1<-graph.adjacency(adjm1); 
    plot(g1)
    

    enter image description here

    P.s. ?graph.adjacency has a lot of good examples (remember to run library('igraph')).

    Related threads

    1. Creating co-occurrence matrix
    2. Co-occurrence matrix using SAC?
    0 讨论(0)
  • 2020-12-16 21:16

    The problem seems to be due to the data-type of the matrix elements. graph.adjacency expects elements of type numeric. Not sure if its a bug.

    After you do,

    m <- as.matrix(dat)
    

    set its mode to numeric by:

    mode(m) <- "numeric"
    

    And then do:

    net <- graph.adjacency(m, mode = "undirected", weighted = TRUE, diag = FALSE)
    > E(net)$weight
    [1]  8  1 10  1 15  1  1  5  7  1
    
    0 讨论(0)
提交回复
热议问题