Skip to content

Commit

Permalink
modified subset_clusters_by_id
Browse files Browse the repository at this point in the history
  • Loading branch information
finlaycampbell committed Jun 6, 2019
1 parent efb9c81 commit f3463b6
Showing 1 changed file with 22 additions and 25 deletions.
47 changes: 22 additions & 25 deletions R/subset_clusters_by_id.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,28 @@

subset_clusters_by_id <- function(x, id){

# Convert epicontacts object to igraph and get linelist + contacts dataframes
net <- as.igraph.epicontacts(x)
net_linelist <- igraph::as_data_frame(net, "vertices")
net_contacts <- igraph::as_data_frame(net, "edges")

# Get cluster information for each node/case
cs <- igraph::clusters(net)
net_nodes <- data.frame(nodes =igraph::V(net)$id,
cs_member = cs$membership,
stringsAsFactors = FALSE)

# Identify cluster containing nodes/cases of interest
cluster_to_subset <- unique(net_nodes$cs_member[which(net_nodes$nodes %in% id)])

# Identify members of cluster belonging to nodes/cases of interest
id_to_subset <- net_nodes$nodes[ which(net_nodes$cs_member %in% cluster_to_subset)]
# Create new epicontacts object with cluster members
new_linelist <- net_linelist[ net_linelist$name %in% id_to_subset, ]
new_contacts <- net_contacts[ net_contacts$from %in% id_to_subset |
net_contacts$to %in% id_to_subset, ]

# Return new epicontacts object
epi_subset <- make_epicontacts(new_linelist, new_contacts, directed = x$directed)

return(epi_subset)
## Convert epicontacts object to igraph and get linelist + contacts dataframes
net <- as.igraph.epicontacts(x)

## Get cluster information for each node/case
cs <- igraph::clusters(net)
net_nodes <- data.frame(nodes =igraph::V(net)$id,
cs_member = cs$membership,
stringsAsFactors = FALSE)

## Identify cluster containing nodes/cases of interest
cluster_to_subset <- unique(net_nodes$cs_member[which(net_nodes$nodes %in% id)])

## Identify members of cluster belonging to nodes/cases of interest
id_to_subset <- net_nodes$nodes[ which(net_nodes$cs_member %in% cluster_to_subset)]

## Subset linelist and contacts by ids
epi_subset <- x[i = id_to_subset,
j = id_to_subset,
contacts = 'both']

return(epi_subset)

}


Expand Down

0 comments on commit f3463b6

Please sign in to comment.