如果您从 R 中使用 igraph,请使用此选项
as_data_frame {igraph} | R 文档 |
此函数从一个或两个包含(符号)边列表和边/顶点属性的数据帧创建 igraph 图。
as_data_frame(x, what = c("edges", "vertices", "both"))
graph_from_data_frame(d, directed = TRUE, vertices = NULL)
from_data_frame(...)
x |
一个 igraph 对象。 |
what |
字符常量,用于返回关于顶点、边或两者信息。默认值为“edges”。 |
d |
一个数据帧,在前两列中包含符号边列表。其他列被视为边属性。自 0.7 版本以来,此参数被强制转换为带有 |
有向 |
逻辑标量,指示是否创建有向图。 |
vertices |
一个带有顶点元数据的数据帧,或者 |
... |
传递给 |
graph_from_data_frame
从一个或两个数据帧创建 igraph 图。它有两种操作模式,具体取决于 vertices
参数是否为 NULL
。
如果 vertices
为 NULL
,则 d
的前两列用作符号边列表,其他列用作边属性。属性名称取自列的名称。
如果 vertices
不为 NULL
,则它必须是一个提供顶点元数据的数据帧。假定 vertices
的第一列包含符号顶点名称,这将作为 ‘name
’ 顶点属性添加到图形中。其他列将添加为其他顶点属性。如果 vertices
不为 NULL
,则检查 d
中给出的符号边列表是否仅包含 vertices
中列出的顶点名称。
通常,数据帧从某些电子表格软件(如 Excel)导出,并通过 read.table
、read.delim
或 read.csv
导入到 R 中。
数据帧中的所有边都包含在图中,这可能包括多个平行边和循环。
as_data_frame
将 igraph 图转换为一个或多个数据帧,具体取决于 what
参数。
如果 what
参数为 edges
(默认值),则返回图的边以及边属性。这些边将位于前两列中,命名为 from
和 to
。(这也表示有向图的边方向。)对于命名图,顶点名称将包含在这些列中,对于其他图,则包含数字顶点 ID。边属性将位于其他列中。不建议使用名为 from
或 to
的边属性,因为这样数据帧中命名的列将不是唯一的。这些边按其数字 ID 的顺序排列。
如果 what
参数为 vertices
,则返回顶点属性。顶点按其数字顶点 ID 的顺序排列。
如果 what
参数为 both
,则返回顶点和边数据,在一个带有命名条目 vertices
和 edges
的列表中。
graph_from_data_frame
的 igraph 图对象,以及 as.data.frame
的数据帧或命名为 edges
和 vertices
的两个数据帧的列表。
对于 graph_from_data_frame
,在创建图之前,‘d’ 的前两列中的 NA
元素被替换为字符串“NA”。这意味着所有 NA
将对应于单个顶点。
‘vertices’ 的第一列中的 NA
元素也被替换为字符串“NA”,但 ‘vertices’ 的其余部分不会被触及。换句话说,顶点名称(=第一列)不能为 NA
,但其他顶点属性可以。
Gabor Csardi csardi.gabor@gmail.com
graph_from_literal
用于创建图的另一种方式,read.table
用于从文件中读取表。
## A simple example with a couple of actors
## The typical case is that these tables are read in from files....
actors <- data.frame(name=c("Alice", "Bob", "Cecil", "David",
"Esmeralda"),
age=c(48,33,45,34,21),
gender=c("F","M","F","M","F"))
relations <- data.frame(from=c("Bob", "Cecil", "Cecil", "David",
"David", "Esmeralda"),
to=c("Alice", "Bob", "Alice", "Alice", "Bob", "Alice"),
same.dept=c(FALSE,FALSE,TRUE,FALSE,FALSE,TRUE),
friendship=c(4,5,5,2,1,1), advice=c(4,5,5,4,2,3))
g <- graph_from_data_frame(relations, directed=TRUE, vertices=actors)
print(g, e=TRUE, v=TRUE)
## The opposite operation
as_data_frame(g, what="vertices")
as_data_frame(g, what="edges")