0

I seem to finally have managed to generate of all cliques in a graph (I had to install R for this, all new to me!).

However now I need to export this list of cliques and I don't know how to tackle this. My code is as follow:

    wt=read.table("data.txt")
wt1=matrix(nrow=nrow(wt), ncol=2)     
wt1=data.frame(wt1)
wt1[,1:2]=wt[,1:2]      
write.table(wt1,"test.txt")
library(igraph)
wt=read.table("test.txt")
wg7<- graph.edgelist(cbind(as.character(wt$X1), as.character(wt$X2)),
                 directed=F)
sum(clusters(wg7)$csize>2)        
plot(wg7)
Allcliks <- cliques(wg7, min = NULL, max = NULL)

Can anybody help me to find the code which will export this list in an excel file? Ideally I would like to have each clique on a new row.

In advance Many Thanks!!!

tweedi
  • 125
  • 5

1 Answers1

1

Solution suggested by @user20650 in the comments bellow.

General data:

library(igraph)
g <- sample_gnp(10, 0.3)
cl <- cliques(g)

Write cl to a csv file, that can be opened in Excel:

for(i in seq_along(cl)){
  cat(paste0(cl[[i]], collapse=","), file="cliques.csv", append = TRUE, sep="\n") 
} 
Paulo MiraMor
  • 1,582
  • 12
  • 30
  • Thanks Paulo! However this throws me the following error message: "Error in WriteXLS(Allclicks, ExcelFileName = "Allclicks.xls") : object 'Allclicks' not found" I don't understand because Allcliks does exist and I can see the values in it :/ – tweedi Jan 23 '17 at 22:39
  • @tweedi Updated the post. The problem is that your object names is `Allcliks`, and not 'Allclicks'. – Paulo MiraMor Jan 23 '17 at 22:42
  • Well spotted. Now I get this one:"Error in get(x, envir = envir) : object '1' not found". I am making research on how to store the list. I wonder if there is not a format issue? – tweedi Jan 23 '17 at 22:52
  • Now, you need to provide a sample data, so that we can understand the structure of `Allcliks`. Maybe generate some random data in the same format, or show us the output of `dput()`. – Paulo MiraMor Jan 23 '17 at 22:55
  • The cliques seem contained in the c(...) strings, the shorter is of length 1 and the longest cliques contains 10 values. Here is a picture: http://www.hostingpics.net/viewer.php?id=472134data.png – tweedi Jan 23 '17 at 23:04
  • Take a look at [how to produce a good question](http://stackoverflow.com/help/how-to-ask) and (how to ask)[http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#5963610]. We'll be glad to help, but we need some sample data to work on. – Paulo MiraMor Jan 23 '17 at 23:08
  • @user20650 I totally agree. I didn't notice `Allcliks` was a list. Please, post your answer, and I'll delete mine, it doesn't make sense anymore. – Paulo MiraMor Jan 23 '17 at 23:16
  • The initial file is an excel matrix with some X where the number of the column matches the number of the row. I have a macro which scans the matrix and retrieve me all pairs of 2. There are 133 pairs of 2 values (which can be from 1 to 47). I then put these 133 pairs one on top of the other in a txt.file and I loaded the file with the read(file.choose()) command. Then I run the code which I put above in order to generate the cliques, and now I would like to learn how to extract them. – tweedi Jan 23 '17 at 23:19
  • @tweedi The answer is updated, but please, make sure that in the next time you make a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). It will avoid spending time and a lot of confusion. – Paulo MiraMor Jan 23 '17 at 23:28
  • @tweedi If this answer solved your problem, please consider marking it as [accepted](http://stackoverflow.com/help/accepted-answer). – Paulo MiraMor Jan 25 '17 at 01:26