Aybid
Aybid

Reputation: 86

Print cliques using R igraph library

I want to print cliques for a graph using igraph of R package. The format of data I want to print as A B C (showing this data in Res1, Res2, Res3 format...)

Data: Res1 Res2 Weight A B 10 A C 1 C B 10 S B 1 L A 2

library(igraph)
file <- read.table("GraphDemo.net", header=TRUE)
graph <- graph.data.frame(file,  directed=F)
Cliq <- cliques(graph, min = 3, max = NULL)

If we want to print the Cliq on the terminal

Cliq

[[1]] + 3/5 vertices, named: [1] A C B

Which is all very good. But if we want to print to file:

write.table(t(Cliq), file="demo.dat",sep = "\t",quote=F,row.names = FALSE)

But the result from file is: V1 c(1, 2, 5)

I want to print data as just the node names A B C. What is the way out guys..!!

Upvotes: 0

Views: 567

Answers (1)

paqmo
paqmo

Reputation: 3739

Use as_ids() to convert the igraph.vs object to a vector of names. You can them compile these into a list and export as you see fit.

Try:

g <- erdos.renyi.game(10,0.5,type="gnp",directed=F)
cliq<-cliques(g,min=3)
V(g)$name <- c("a","b", "c","d","e","f","g","h","i","j")
#Here's the function that will get the vertex names
names <- lapply(1:length(cliq), function(x) as_ids(cliq[[x]]))

Now, this extracts all the cliques. If you are interested in only clique of size 3, e.g., you can restrict that using the cliques() call or the lapply() function.

Upvotes: 3

Related Questions