vankappa
vankappa

Reputation: 81

R and igraph: deleting edges with weights between two values

This is a stupid question, but here we are: I have a crosscorrelation matrix, like this one:

> dput(cmat2)
structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0.892305552010683, 
0.683880877230643, 0.82509656411663, 0.666360444688385, 1, 0.892305552010683, 
0.683880877230643, 0.825096563824574, 0, 0.892305552010683, 1, 
0.692861822147646, 0.838251810637771, 0.698923895580299, 0.892305552010683, 
1, 0.692861822147646, 0.838251810387625, 0, 0.683880877230643, 
0.692861822147646, 1, 0.619756696903943, 0.511941092682266, 0.683880877230643, 
0.692861822147646, 1, 0.619756697865856, 0, 0.82509656411663, 
0.838251810637771, 0.619756696903943, 1, 0.631621405554924, 0.82509656411663, 
0.838251810637771, 0.619756696903943, 0.999999999999996, 0, 0.666360444688385, 
0.698923895580299, 0.511941092682266, 0.631621405554924, 1, 0.666360444688385, 
0.698923895580299, 0.511941092682266, 0.631621406119761, 0, 1, 
0.892305552010683, 0.683880877230643, 0.82509656411663, 0.666360444688385, 
1, 0.892305552010683, 0.683880877230643, 0.825096563824574, 0, 
0.892305552010683, 1, 0.692861822147646, 0.838251810637771, 0.698923895580299, 
0.892305552010683, 1, 0.692861822147646, 0.838251810387625, 0, 
0.683880877230643, 0.692861822147646, 1, 0.619756696903943, 0.511941092682266, 
0.683880877230643, 0.692861822147646, 1, 0.619756697865856, 0, 
0.825096563824574, 0.838251810387625, 0.619756697865856, 0.999999999999996, 
0.631621406119761, 0.825096563824574, 0.838251810387625, 0.619756697865856, 
1), .Dim = c(10L, 10L))

which I use as weights for a graph:

graph <- graph.adjacency(as.matrix(cmat), weighted = TRUE)

Now, my intention is to select only the edges with a weight BETWEEN -0.15 and 0.65. While I can select one of the two options (above 0.65 OR above -0.15), but not both. I tried:

graph <- delete.edges(graph, which(E(graph)$weight > -0.15) & which(E(graph)$weight <0.65))

but this leads to an endless loading, when trying to display the graph with ggraph

Cheers

Upvotes: 2

Views: 826

Answers (2)

clp
clp

Reputation: 1528

Or subtract the edges as derived by ThomasisCoding from the graph with the 'minus' operator:

graph - E(graph)[E(graph)$weight > -0.15 & E(graph)$weight < 0.65]

Upvotes: 3

ThomasIsCoding
ThomasIsCoding

Reputation: 102890

You can try either approach from below:

graph_from_data_frame(
    subset(
        get.data.frame(graph),
        !(weight > -0.15 & weight < 0.65)
    )
)

or

delete.edges(
    graph,
    E(graph)[E(graph)$weight > -0.15 & E(graph)$weight < 0.65]
)

Upvotes: 4

Related Questions