[R] For Social Network Analysis-Graph Analysis - How to convert 2 mode data to 1 mode data?

Gabor Csardi csardi at rmki.kfki.hu
Wed May 14 10:01:57 CEST 2008


Please stay on the list.

On Tue, May 13, 2008 at 06:05:15PM -0400, Messing, Solomon O. wrote:
> Gabor,
> 
> By the way, this seems to work:

I'm a bit lost. So now you're converting your data frame 
to a matrix? Why? Or you're doing the two-mode to one-mode
conversion here? It does not seem so to me.

Btw. there is a get.adjacency function in igraph to convert 
a graph to an adjacency matrix.

G.

> 
> df.to.nxn <- function( x, y ){
> # x values will be the N x N values   
>     M <- matrix( nrow = length( unique( x ) ), ncol = length( unique( x
> ) ), 
>           dimnames = list( unique( x ), unique( x ) ) )
>     M[ 1:length( unique( x ) ), 1:length( unique( x ) ) ] <- 0
> # initialize the values to 0 
>     for( i in 1:length( x ) ) {
> # iterate through rows of data    
>             index = which( y == y[i] )                    
>                 M[ as.character( x[ index ] ), as.character( x[ index ]
> ) ] = 1        
>      }
> M
> # return M, an N x N matrix
> }

-- 
Csardi Gabor <csardi at rmki.kfki.hu>    UNIL DGM



More information about the R-help mailing list