wake_wake
wake_wake

Reputation: 1204

How do I select nodes in a network object based on their edge value?

This might be a very straight forward question, but I seem not to be able to work it out.

In R I have a network object of 251739 nodes (inventors) and 759804 edges (collaborations on patents). Both nodes and edges have attribute files. One of such a edge attribute is appyear, i.e. the year the inventors applied for a patent.

I want to write all nodes that are incidence on a patent for which appyear == 2005 to a new network.

Can someone give me some pointer on how to do this? I use the latest version of R and the STATNET package.

Upvotes: 2

Views: 2305

Answers (2)

skyebend
skyebend

Reputation: 1109

Version 1.11 of the statnet 'network' package added an eid argument to the get.inducedSubgraph() function. So should be able to directly extract a network containing just the matching edges, almost the same as the igraph example.

Create sample dataset, a network named 'authors':

m <- matrix(sample(0:1,625,replace=T),nc=25)
diag(m) <- 0
authors<-as.network(m)
authors%e%'appyear' <- sample(2000:2015,network.edgecount(authors),replace=T)

Extract a new network object containing just the edges where 'appyear' == 2005

authors2005<-get.inducedSubgraph(authors,eid=which(authors%e%'appyear'==2005))

Upvotes: 1

jlhoward
jlhoward

Reputation: 59385

So here's a method using the igraph package.

library(igraph)
# create an igraph with 25 authors; you have this already...
set.seed(1)
m <- matrix(sample(0:1,625,replace=T),nc=25)
diag(m) <- 0      # authors don't collaborate with themselves
g <- graph.adjacency(m,weight=T)                      # create the graph
E(g)$appyear <- sample(2000:2015,ecount(g),replace=T) # create an edge attribute "appyear"

# you start here...
g.new <- subgraph.edges(g,eids=which(E(g)$appyear==2005))
par(mfrow=c(1,2))
plot(g)
plot(g.new)

statnet uses objects of class network, so we convert at the end.

library(network)
new.network <- as.network(get.edgelist(new.g))

Upvotes: 3

Related Questions