Pon4a
Pon4a

Reputation: 27

How to add edges from one graph to another graph in R?

I'm using R (v3.4.1). I have a graphml file for a graph:

g <-read.graph(file = "./proteinLC.graphml",format = "graphml")

I need to get 10% of nodes from graph g and put them to graph m. I tried to do something like this:

m <- add_edges(g, c(sample(1:length(E(g)), length(E(g))*0.1, replace = F)))

But I get an error:

Error: At type_indexededgelist.c:272 : cannot add edges, Invalid vertex id**

What am I doing wrong?

Upvotes: 0

Views: 116

Answers (1)

G5W
G5W

Reputation: 37641

Despite your title, I don't think the way to do this is to add edges. Instead, there is a built-in function to get a subgraph from a list of nodes. Here is an example.

library(igraph)

## Build some test data
set.seed(2017)
G = erdos.renyi.game(200, 0.2)
plot(G)

## Too big,  want a sample
Samp = sample(V(G), 0.1*length(V(G)))
m = induced_subgraph(G, Samp)
plot(m)

Sampled graph

Upvotes: 1

Related Questions