R语言学习-提取igraph的节点和边

来源:互联网 发布:gcc编译器下载 linux 编辑:程序博客网 时间:2024/06/16 01:28
网络分析的时候,可能需要提取出网络中的节点或者边,igraph包中其实提供了很多可用的函数。#创建网络方法之一:data.framedata<-data.frame(id1=c(1,1,2,3,4,4,5,5,6,6,7,8,8,9,10,5,15,6,7,16),id2=c(2,11,11,12,13,14,15,16,7,15,16,17,18,18,9,19,19,19,19,19))g <- graph_from_data_frame(data, directed=FALSE)    #directed 参数控制graph 有无方向gIGRAPH UN-- 16 17 -- + attr: name (v/c)+ edges (vertex names): [1] 1 --2  2 --3  3 --4  1 --4  5 --7  5 --6  5 --8  7 --6  7 --8  6 --8  9 --10 9 --13 11--10 11--12 12--13 14--15 1 --16#图形显示plot(g)

#V(g)和E(g)可以用来查看网络g的节点和边 V(g)+ 16/16 vertices, named: [1] 1  2  3  5  7  6  9  11 12 14 16 4  8  10 13 15E(g)+ 17/17 edges (vertex names): [1] 1 --2  2 --3  3 --4  1 --4  5 --7  5 --6  5 --8  7 --6  7 --8  6 --8  9 --10 9 --13 11--10 11--12 12--13 14--15 1 --16#但问题是怎么将里面的数据提取出来放到变量里面呢?#节点提取有个函数get.vertex.attribute(g)get.vertex.attribute(g)$name [1] "1"  "2"  "3"  "5"  "7"  "6"  "9"  "11" "12" "14" "16" "4"  "8"  "10" "13" "15"#查看类型可知是listclass(get.vertex.attribute(g))[1] "list"#剩下的就简单了node<-get.vertex.attribute(g)[[1]]node [1] "1"  "2"  "3"  "5"  "7"  "6"  "9"  "11" "12" "14" "16" "4"  "8"  "10" "13" "15"#至于边呢?可以使用get.edgelist()get.edgelist(g)      [,1] [,2] [1,] "1"  "2"  [2,] "2"  "3"  [3,] "3"  "4"  [4,] "1"  "4"  [5,] "5"  "7"  [6,] "5"  "6"  [7,] "5"  "8"  [8,] "7"  "6"  [9,] "7"  "8" [10,] "6"  "8" [11,] "9"  "10"[12,] "9"  "13"[13,] "11" "10"[14,] "11" "12"[15,] "12" "13"[16,] "14" "15"[17,] "1"  "16"#类型是matrix矩阵可以直接使用class(get.edgelist(g))[1] "matrix"


原创粉丝点击