从 data.frame 创建邻接列表

发布于 2024-07-26 22:39:18 字数 126 浏览 4 评论 0原文

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

必须有一个很好的单行线来将此 data.frame 转换为邻接关系列表。 有什么提示吗?

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温柔少女心 2024-08-02 22:39:18

既然您标记了此 ,那么使用内置功能怎么样?功能?

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

唯一需要注意的是顶点索引为零,这将在 igraph 0.6 中发生变化。

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

> 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.

嘿看小鸭子会跑 2024-08-02 22:39:18

又快又脏……

> 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

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
放手` 2024-08-02 22:39:18
> 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
> 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
怂人 2024-08-02 22:39:18

你会如何在 R 中表示邻接表? 它需要可变大小的列表来存储相邻节点的集合; 所以你必须使用 list(); 但是在 R 中使用它有什么好处呢?

我可以想到类似 sapply 的函数的蹩脚技巧,但它们对每个节点进行线性扫描。 但玩了一分钟,这里是:一个配对列表的列表,其中每对的第二项是邻接列表。 输出比数据结构实际更疯狂。

> 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()

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?

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.

> 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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文