Reputation: 1216
I have a list of undirected graph edges like follows:
A B
B A
C D
I would like to extract unique edges.
So the end result would return only
A B (or) B A
C D
I know how to do this in python, and how to select unique rows in R. But how would I do this additional constraint in R.
Upvotes: 1
Views: 224
Reputation: 206283
If you are working with graph data, you might also consider using the igraph
package in R. If this is your input data
dd<-matrix(c("A", "B", "C", "B", "A", "D"), ncol=2)
# [,1] [,2]
# [1,] "A" "B"
# [2,] "B" "A"
# [3,] "C" "D"
Then you could do
library(igraph)
gg<-simplify(graph.edgelist(dd, directed=F))
get.edgelist(gg)
# [,1] [,2]
# [1,] "A" "B"
# [2,] "C" "D"
Here the simplify
remove the redundant bidirectional edge from A-B. Then you can also do fun stuff like plot it
plot(gg)
Upvotes: 1
Reputation: 1336
Maybe there's a better way, but one option would be to sort the data so that each row is in alphabetical order, so that rows containing the same items will become identical, and using unique
on the result:
edges <- data.frame(a=c("A", "B", "C"), b=c("B","A","D"))
unique.edges <- unique(t(apply(edges,1, sort)) )
output here is a character matrix like:
# [,1] [,2]
#[1,] "A" "B"
#[2,] "C" "D"
Upvotes: 2