从data.frame创建邻接列表

时间:2022-09-23 17:52:25

I have a data.frame with 2 columns: Node A, Node B. Each entry in the frame implies an edge in a graph between node A and B.

我有一个包含2列的data.frame:节点A,节点B.帧中的每个条目都意味着节点A和节点B之间的图形边缘。

There must be a nice one-liner to convert this data.frame into an adjacency list. Any hints?

必须有一个很好的单行将这个data.frame转换成邻接列表。任何提示?

4 个解决方案

#1


6  

Quick and dirty ...

又快又脏......

> edges <- data.frame(nodea=c(1,2,4,2,1), nodeb=c(1,2,3,4,5))

> adjlist <- by(edges, edges$nodea, function(x) x$nodeb)

> for (i in as.character(unique(edges$nodea))) {
+   cat(i, ' -> ', adjlist[[i]], '\n')
+ }

1  ->  1 5
2  ->  2 4
4  ->  3

> adjlist
edges$nodea: 1
[1] 1 5
------------------------------------------------------------
edges$nodea: 2
[1] 2 4
------------------------------------------------------------
edges$nodea: 4
[1] 3

#2


11  

Since you tagged this , how about using built in functionality?

既然你标记了这个igraph,那么使用内置功能怎么样?

> g <- graph.data.frame( edges )
> adjlist <- get.adjedgelist(g)

Only caveat is the vertices are zero indexed, which will be changing with igraph 0.6.

唯一需要注意的是顶点是零索引的,它将随着igraph 0.6而变化。

#3


4  

> edges <- data.frame(nodea=c(1,2,4,2,1), nodeb=c(1,2,3,4,5))

> attach(edges)

> tapply(nodeb,nodea,unique)

$`1`
[1] 1 5

$`2`
[1] 2 4

$`4`
[1] 3

#4


0  

how would you even represent an adjacency list in R? it needs variable-sized lists for the set of adjacent nodes; so then you have to use a list(); but then what good is it having it in R?

你怎么会在R中代表邻接名单?它需要一组相邻节点的可变大小的列表;所以你必须使用list();但是它在R中有什么用呢?

i can think of lame tricks with sapply-like functions but they do a linear scan for every node. but playing around for 1 minute, here is: a list of pairlists, where the second item of each pair is the adjacency list. output is crazier than the datstructure really is.

我可以想到具有类似蓝宝石功能的蹩脚技巧,但他们会对每个节点进行线性扫描。但是玩了1分钟,这里是:一个pairlist列表,其中每对的第二项是邻接列表。输出比datstructure真的更疯狂。

> edgelist=data.frame(A=c(1,1,2,2,2),B=c(1,2,2,3,4))
> library(plyr)
> llply(1:max(edgelist), function(a) list(node=a, adjacents=as.list(edgelist$B[edgelist$A==a])))
[[1]]
[[1]]$node
[1] 1

[[1]]$adjacents
[[1]]$adjacents[[1]]
[1] 1

[[1]]$adjacents[[2]]
[1] 2



[[2]]
[[2]]$node
[1] 2

[[2]]$adjacents
[[2]]$adjacents[[1]]
[1] 2

[[2]]$adjacents[[2]]
[1] 3

[[2]]$adjacents[[3]]
[1] 4



[[3]]
[[3]]$node
[1] 3

[[3]]$adjacents
list()


[[4]]
[[4]]$node
[1] 4

[[4]]$adjacents
list()

#1


6  

Quick and dirty ...

又快又脏......

> edges <- data.frame(nodea=c(1,2,4,2,1), nodeb=c(1,2,3,4,5))

> adjlist <- by(edges, edges$nodea, function(x) x$nodeb)

> for (i in as.character(unique(edges$nodea))) {
+   cat(i, ' -> ', adjlist[[i]], '\n')
+ }

1  ->  1 5
2  ->  2 4
4  ->  3

> adjlist
edges$nodea: 1
[1] 1 5
------------------------------------------------------------
edges$nodea: 2
[1] 2 4
------------------------------------------------------------
edges$nodea: 4
[1] 3

#2


11  

Since you tagged this , how about using built in functionality?

既然你标记了这个igraph,那么使用内置功能怎么样?

> g <- graph.data.frame( edges )
> adjlist <- get.adjedgelist(g)

Only caveat is the vertices are zero indexed, which will be changing with igraph 0.6.

唯一需要注意的是顶点是零索引的,它将随着igraph 0.6而变化。

#3


4  

> edges <- data.frame(nodea=c(1,2,4,2,1), nodeb=c(1,2,3,4,5))

> attach(edges)

> tapply(nodeb,nodea,unique)

$`1`
[1] 1 5

$`2`
[1] 2 4

$`4`
[1] 3

#4


0  

how would you even represent an adjacency list in R? it needs variable-sized lists for the set of adjacent nodes; so then you have to use a list(); but then what good is it having it in R?

你怎么会在R中代表邻接名单?它需要一组相邻节点的可变大小的列表;所以你必须使用list();但是它在R中有什么用呢?

i can think of lame tricks with sapply-like functions but they do a linear scan for every node. but playing around for 1 minute, here is: a list of pairlists, where the second item of each pair is the adjacency list. output is crazier than the datstructure really is.

我可以想到具有类似蓝宝石功能的蹩脚技巧,但他们会对每个节点进行线性扫描。但是玩了1分钟,这里是:一个pairlist列表,其中每对的第二项是邻接列表。输出比datstructure真的更疯狂。

> edgelist=data.frame(A=c(1,1,2,2,2),B=c(1,2,2,3,4))
> library(plyr)
> llply(1:max(edgelist), function(a) list(node=a, adjacents=as.list(edgelist$B[edgelist$A==a])))
[[1]]
[[1]]$node
[1] 1

[[1]]$adjacents
[[1]]$adjacents[[1]]
[1] 1

[[1]]$adjacents[[2]]
[1] 2



[[2]]
[[2]]$node
[1] 2

[[2]]$adjacents
[[2]]$adjacents[[1]]
[1] 2

[[2]]$adjacents[[2]]
[1] 3

[[2]]$adjacents[[3]]
[1] 4



[[3]]
[[3]]$node
[1] 3

[[3]]$adjacents
list()


[[4]]
[[4]]$node
[1] 4

[[4]]$adjacents
list()