简体   繁体   English

igraph和read.table,R中的孤立顶点

[英]igraph and read.table, isolated vertices in R

I saw that igraph in R requires data structured like this: 我看到R中的igraph需要这样的数据结构:

nodeA  nodeB   int_1 int_2
AA      BD      6   X   
BD      CA      8   Y
AA      DE      7   Y
...     ...     ... ...

And I saw that through 我看到了

data<-read.table(file)
graph.data.frame(data)

I obtain the corresponding network. 我获得了相应的网络。

Now say I have to put in isolated nodes, I searched in the documentation but could not find anything that answered my issue. 现在说我必须放置隔离的节点,我在文档中进行了搜索,但是找不到任何可以解决我的问题的东西。

How can I specify them in the original file? 如何在原始文件中指定它们?

I thought of something like (like a .sif format) 我想到了类似的东西(如.sif格式)

nodeA  nodeB   int_1 int_2
AA      DE      7   Y
...     ...     ... ...
isoNodeA
isoNodeB
...

but obviously the read.table does not accept different number of fields between rows. 但显然read.table不能接受行之间不同数量的字段。

You could try it like this: 您可以这样尝试:

data<-read.table(header=T, fill = TRUE, stringsAsFactors=F, text="
nodeA  nodeB   int_1 int_2
AA      BD      6   X   
BD      CA      8   Y
AA      DE      7   Y
ZZ
DE      BD      7   Y")
data[data==""] <- NA

library(igraph)
g <- graph.data.frame(
  data[complete.cases(data),], 
  vertices = unique(na.omit(unlist(data[1:2]))))
plot(g)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM