1

I have a dataset that looks like this

firm1 firm2
A      B   
A      D  
A      G   
B      A
D      A
G      A

As you can see, it gives out unique information, but the combinations are still the same.

I would like to filter out the unique combinations, so my dataset looks like this.

firm1 firm2
A      B   
A      D  
A      G   

How can I filter out these repeated (but reversed) combinations?

ineedhelp
  • 23
  • 1
  • 8

2 Answers2

0

I would suggest this base R approach:

#Data
df <- structure(list(firm1 = c("A", "A", "A", "B", "D", "G"), firm2 = c("B", 
"D", "G", "A", "A", "A")), row.names = c(NA, -6L), class = "data.frame")

The code:

df[!duplicated(lapply(strsplit(paste0(df$firm1,df$firm2),split = ''),sort)),]

Output:

  firm1 firm2
1     A     B
2     A     D
3     A     G
Duck
  • 39,058
  • 13
  • 42
  • 84
0

Here is a solution using igraph package

library(igraph)
setNames(unique(as_data_frame(graph_from_data_frame(df, directed = FALSE))), names(df))

which gives

  firm1 firm2
1     A     B
2     A     D
3     A     G
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81