为 Gephi 准备数据

Preparing data for Gephi

问候,

我需要为 Gephi 中的网络分析准备数据。我有以下格式的数据:

MY Data

我需要以下格式的数据(其中值代表通过组织联系的人员):

Required format

非常感谢!

x 开始:

structure(list(Persons = c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L), Organizations = c("A", "B", "E", "F", "A", "E", "C", "D", "C", "A", "E")), .Names = c("Persons", "Organizations"), class = "data.frame", row.names = c(NA,-11L))

使用不同的名称创建一个新的 data.frame。只需将 Organizations 转换为一个因数,然后使用数值:

> y=data.frame(Source=x$Persons, Target=as.numeric(as.factor(x$Organizations)))
> y
   Source Target
1       1      1
2       1      2
3       1      5
4       2      6
5       2      1
6       2      5
7       2      3
8       3      4
9       3      3
10      3      1
11      3      5

无论如何,我很确定 gephi 可以处理字符串。

我认为这段代码应该可以完成这项工作。这不是最好最优雅的方式,但它确实有效:)

# Data
x <-
  structure(
    list(
      Persons = c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L),
      Organizations = c("A", "B", "E", "F", "A", "E", "C", "D", "C", "A", "E")
    ),
    .Names = c("Persons", "Organizations"),
    class = "data.frame",
    row.names = c(NA, -11L)
  )

# This will merge n:n
edgelist <- merge(x, x, by = "Organizations")[,2:3]

# We don't want autolinks
edgelist <- subset(edgelist, Persons.x != Persons.y)

# Removing those that are repeated
edgelist <- unique(edgelist)

edgelist
#>   Persons.x Persons.y
#> 2         1         3
#> 3         1         2
#> 4         3         1
#> 6         3         2
#> 7         2         1
#> 8         2         3

reprex 创建于 2018-01-03 包 (v0.1.1.9000).