R中约束{igraph}的输出文件

The output file of constraint {igraph} in R

我在 R 中使用 igraph 包来计算每个顶点的 Burt 约束 (http://www.inside-r.org/packages/cran/igraph/docs/constraint)。

我通过 运行 命令 constraint(g, weights=NULL) 得到的输出如下:

       vertex1    vertex2    vertex3    vertex4    vertex5    vertex6    vertex7
    1.00000000 0.50000000 0.50000000 1.00000000 1.00000000 1.00000000 0.29986736
       vertex8    vertex9   vertex10   vertex11   vertex12   vertex13   vertex14
    1.00000000 1.00000000 0.25000000 1.00000000 1.00000000 0.50000000 1.00000000
       ...
    vertex1000
    0.25000000

我的问题是如何将其导出到具有以下结构的 xlsx(或 csv、txt)文件中:

vertex1 1.00000000
vertex2 0.50000000
vertex3 0.50000000
...
vertex1000 0.25000000

非常感谢您的解决方案。

如果将结果保存到变量 x,请尝试

write.csv( data.frame(vertex=names(x), value=x), "out.csv", row.names=FALSE)

感谢您的意见 MrFlick。我找到了答案。它比仅使用 data.frame 稍微复杂一点。我 post 将 Burt 的约束结果从 R 中的 igraph 包转换为下面的顶点约束形式的代码:

library("igraph")
library("reshape2")
edge_list=read.delim("your_valued_edgelist.txt")
g=graph.data.frame(edge_list, directed=FALSE, vertices=NULL)
x<-capture.output(melt(constraint(g, weights=NULL)))
x$id <- rownames(x)
x[1] <- NULL
y<-data.frame(vertex_id=substr(x,1,6), constraint_score=as.numeric(substr(x,8,13)))
write.table(y, file = "your_outcome.txt", col.names = TRUE, row.names = FALSE, sep="\t")

在我的例子中,我必须 substring 我的 vertex_id 前六个字符。