R/communitytable.R

Defines functions communitytable

Documented in communitytable

#'Create a table of structurally based communities
#'
#'Function for generating a table of the communities generated by the Girvan-Newman edge betweenness community
#'detection algorithm.
#'
#'@param edgelist a dataframe that contains a list of people and their associates. The first column represents
#'source nodes and the second column represents target nodes.
#'
#'@examples
#'# minimal example
#'source <- c("a", "a", "b", "c", "d", "d", "d")
#'target <- c("b", "c", "c", "d", "e", "f", "g")
#'associations <- data.frame(source, target)
#'communitytable(edgelist = associations)
#'
#'# FM 3-24 example association matrix
#'data("fm3_24_affiliations")
#'affiliations <- as.data.frame(fm3_24_affiliations)
#'associations <- transform2social(edgelist = affiliations)
#'communitytable(edgelist = associations)
#'
#'@rdname communitytable
#'@export

communitytable <- function(edgelist){
  require(igraph)

  #Create a graph using igraph
  g<-graph_from_edgelist(as.matrix(edgelist[,1:2]),directed=FALSE)
  g<-as.undirected(g,mode="collapse")

  #Identify communities using edge betweenness
  grps<-cluster_edge_betweenness(g)
  df <- data.frame(agent = grps$names, group = grps$membership)
  sortidx <- sort(df$group, decreasing=FALSE, index.return = TRUE)
  sorteddf <- df[sortidx$ix,]
  sorteddf
}
dkoban/SNAToolKit documentation built on May 20, 2019, 1:28 p.m.