Messing, Solomon O.
2008-May-09 19:37 UTC
[R] For Social Network Analysis-Graph Analysis - How to convert 2 mode data to 1 mode data?
Hi, Does anyone know of a package in R that has a function to convert network data (e.g. an adjacency matrix or ) from 2-mode to 1-mode? I am conducting social network analysis. I know that Pajek has this function under Net --> Transform --> 2-mode to 1-mode --> Rows. I have searched the documentation under packages 'sna', 'network', 'igraph', and 'dynamicgraph' but I was not able to identify a comparable function. I would just export my data to Pajek and import it to R, but I'm going to have to generate hundreds of these graphs, so it would take quite a bit of time to do it this way. Thanks, Solomon [[alternative HTML version deleted]]
Gabor Csardi
2008-May-10 17:09 UTC
[R] For Social Network Analysis-Graph Analysis - How to convert 2 mode data to 1 mode data?
Solomon, if i understand two-mode networks properly (they're bipartite, right?), then this is not hard to do with igraph. Basically, for each vertex create an order=2 neighborhood, and then create a graph from the adjacency list, it is something like this: two.to.one <- function(g, keep) { neis <- neighborhood(g, order=2) neis <- lapply(seq(neis), function(x) neis[[x]][ neis[[x]] != x-1]) ## drop self-loops neis <- lapply(neis, function(x) x[ x %in% keep ]) ## keep only these neis <- lapply(seq(neis), function(x) t(cbind(x-1, neis[[x]]))) ## create edge lists neis[-keep-1] <- NULL ## these are not needed neis <- matrix(unlist(neis), byrow=TRUE, nc=2) ## a single edge list neis <- neis[ neis[,1] > neis[,2], ] ## count an edge once only mode(neis) <- "character" g2 <- graph.edgelist(neis, dir=FALSE) V(g2)$id <- V(g2)$name ## 'id' is used in Pajek g2 } It does not check that the graph is indeed two-mode, and it keeps the vertices given in the 'keep' argument. 'keep' is made of igraph vertex ids. You can use it like this: g <- graph.ring(10) keep <- seq(0,8,by=2) ## we keep the 'even' vertices g2 <- two.to.one(g, keep) write.graph(two.to.one(g, keep), format="pajek", file="/tmp/a.net") I haven't tested it much. We'll have a better function in the next igraph version. Gabor On Fri, May 09, 2008 at 03:37:05PM -0400, Messing, Solomon O. wrote:> Hi, > > > > Does anyone know of a package in R that has a function to convert > network data (e.g. an adjacency matrix or ) from 2-mode to 1-mode? I am > conducting social network analysis. I know that Pajek has this function > under Net --> Transform --> 2-mode to 1-mode --> Rows. I have searched > the documentation under packages 'sna', 'network', 'igraph', and > 'dynamicgraph' but I was not able to identify a comparable function. > > > > I would just export my data to Pajek and import it to R, but I'm going > to have to generate hundreds of these graphs, so it would take quite a > bit of time to do it this way. > > > > Thanks, > > > > Solomon > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.-- Csardi Gabor <csardi at rmki.kfki.hu> UNIL DGM
Gabor Csardi
2008-May-14 08:01 UTC
[R] For Social Network Analysis-Graph Analysis - How to convert 2 mode data to 1 mode data?
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
S. Messing
2009-Jun-04 18:28 UTC
[R] For Social Network Analysis-Graph Analysis - How to convert 2 mode data to 1 mode data?
All, There is a simple solution to this problem using R's matrix algebra commands. I describe in detail at http://www.stanford.edu/~messing/Affiliation%20Data.html http://www.stanford.edu/~messing/Affiliation%20Data.html . Best wishes, -Solomon -- View this message in context: http://www.nabble.com/For-Social-Network-Analysis-Graph-Analysis---How-to-convert-2-mode-data-to-1-mode-data--tp17157380p23875403.html Sent from the R help mailing list archive at Nabble.com.