简体   繁体   中英

igraph and read.table, isolated vertices in R

I saw that igraph in R requires data structured like this:

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)

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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