[R] recursive function - finding connections

Benton, Paul hpaul.benton08 at imperial.ac.uk
Fri Jul 15 01:12:00 CEST 2011


Sorry bad example. My data is undirected. It's a correlation matrix so probably better to look at something like:

foomat<-cor(matrix(rnorm(100), ncol=10))
foomat

mine are pvalues from the correlation but same idea.


On 14 Jul 2011, at 11:23, Erich Neuwirth wrote:

> cliques only works for undirected graphs.
> Your matrix is not symmetric, therefore
> the graph is directed.
> 
> 
> On 7/14/2011 8:53 AM, Benton, Paul wrote:
>> Dear all,
>> 
>> I'm having some problems getting my recursive function to work. At first I though that maybe my data was too big and I increase option(expressions=50000). Then I thought that I would try it on some smaller data. Still not working. :( 
>> I would have thought there should be a function for this already, so any suggestions are welcomed for other methods. I did try igraph but couldn't get cliques() to give anything useful. Also a quick play with hclust and cut, again nothing too useful.
>> 
>> Basically the function is trying to find uniquely connected subgraphs. So the sub-network is only connected by itself and not to other nodes. If everything is connected then the list (connectedList) should be length of 1 and have every index in the 1st slot.
>> 
>> cheers,
>> 
>> Paul
>> 
>> 
>> findconnection<-function(mat, cutoff){
>> 	toList<-function(mat, connectList, cutoff, i, idx){
>> 		idx<-which(mat[,idx] < cutoff)
>> 		if(length(idx) >= 1){
>> 			connectList[[i]]<-idx
>> 			for(z in 1:length(idx)){
>> 				connectList<-toList(mat, connectList, cutoff, i, idx[z])
>> 			}
>> 		}else{
>> 			return(connectList)
>> 		}
>> 	}
>> 	
>> 	connectList<-list()
>> 	for(i in 1:ncol(mat)){
>> 		connectList<-toList(mat, connectList, cutoff, i, i)
>> 	}
>> 	return(unique(connectList)) 
>> }
>> 
>> foomat<-matrix(sample(c(1,0.5,0), 100, replace=T), nrow=10) ## example data
>>> foomat
>>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
>> [1,]  0.0  0.5  0.0  0.5  0.5  0.0  0.5  1.0  0.5   0.0
>> [2,]  0.0  1.0  1.0  0.0  0.0  1.0  0.0  1.0  0.5   1.0
>> [3,]  1.0  1.0  1.0  1.0  0.5  0.0  0.5  0.5  0.5   0.5
>> [4,]  0.0  0.5  0.0  0.0  0.5  0.5  0.5  0.0  1.0   0.0
>> [5,]  0.5  0.5  1.0  1.0  0.5  1.0  1.0  0.5  0.5   0.5
>> [6,]  0.0  0.5  0.0  0.5  0.5  0.5  0.5  0.5  1.0   1.0
>> [7,]  1.0  1.0  0.0  1.0  0.0  0.5  1.0  1.0  0.5   0.5
>> [8,]  0.5  1.0  0.0  0.5  1.0  0.0  1.0  0.0  0.0   0.0
>> [9,]  0.0  0.5  0.0  0.0  0.5  0.0  0.5  0.0  0.5   0.5
>> [10,]  1.0  1.0  0.5  1.0  0.0  1.0  0.0  0.0  0.0   0.5
>>> pb<-findconnection(foomat, 0.01)
>> Error: C stack usage is too close to the limit
>> Error during wrapup: 
>> 
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>> 
> 



More information about the R-help mailing list