在R中操纵网络数据

时间:2009-07-23 03:24:26

标签: r networking social-networking

我有一个详细说明N个节点之间边缘权重的数据框。是否有用于处理此类数据的包?

例如,我想将以下信息绘制为网络:

  p1 p2 counts
1  a  b    100
2  a  c    200
3  a  d    100
4  b  c     80
5  b  d     90
6  b  e    100
7  c  d    100
8  c  e     40
9  d  e     60

4 个答案:

答案 0 :(得分:14)

一个选项是network包,它是用于统计社交网络分析的statnet R系列包的一部分。它以稀疏的方式处理网络数据,这对于较大的数据集很有用。

下面,我执行以下操作:

  • 将edgelist(前两列)加载到网络对象中
  • 将计数指定为称为权重的边缘属性。
  • 使用gplot绘制网络。 (请参阅帮助页面以更改边缘的厚度。)
  • 绘制一个社会矩阵(只是一组代表邻接矩阵的5x5块,其中(i,j)单元格用相对计数着色)
A = read.table(file="so.txt",header=T)
A
      p1 p2 counts
    1  a  b    100
    2  a  c    200
    3  a  d    100
    4  b  c     80
    5  b  d     90
    6  b  e    100
    7  c  d    100
    8  c  e     40
    9  d  e     60

library(network)
net = network(A[,1:2])
# Get summary information about your network
net
     Network attributes:
      vertices = 5 
      directed = TRUE 
      hyper = FALSE 
      loops = FALSE 
      multiple = FALSE 
      bipartite = FALSE 
      total edges= 9 
        missing edges= 0 
        non-missing edges= 9 
        Vertex attribute names: 
        vertex.names 
     adjacency matrix:
      a b c d e
    a 0 1 1 1 0
    b 0 0 1 1 1
    c 0 0 0 1 1
    d 0 0 0 0 1
    e 0 0 0 0 0

set.edge.attribute(net,"weight",A[,3])
gplot(net)

## Another cool feature
s = as.sociomatrix(net,attrname="weight")
plot.sociomatrix(s)

答案 1 :(得分:4)

以下是如何在igraph

中制作数据的网络图
d <- data.frame(p1=c('a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'd'),
                p2=c('b', 'c', 'd', 'c', 'd', 'e', 'd', 'e', 'e'),
                counts=c(100, 200, 100,80, 90,100, 100,40,60))

library(igraph)
g <- graph.data.frame(d, directed=TRUE)
print(g, e=TRUE, v=TRUE)
tkplot(g, vertex.label=V(g)$name)

答案 2 :(得分:0)

我也一直在igraph工作。创建图形的一种方法是将所有“从”到“节点”的列表写出到文本文件中,将其作为图形对象读回。图形对象可以经受许多图形理论过程,并且可以处理相当大的网络。

答案 3 :(得分:0)

根据我的经验,igraph是我最喜欢的大型图形理论工作包。它具有内存效率,并且具有一些非常好的算法。 igraph使用类似内部边缘列表的数据结构 对于更简单/更小的东西,我倾向于使用'sna'包(“社交网络分析”)。它非常适合交互式工作和小型网络的绘图。 sna使用了更多的邻接矩阵数据结构。

相关问题