Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pairwise unique combinations ignoring the direction

Tags:

dataframe

r

I want to aggregate a certain value in a data.frame based on a common character in R. The Problem is that I am not interested in different directions of the pairwise combination. So for instance

d = data.frame( x = LETTERS[1:5], y = LETTERS[5:1] )

  x y
1 A E
2 B D
3 C C
4 D B
5 E A

The combination would be then calculated like this:

d$z <- paste0(d$x,d$y,sep="_")

The problem is that i am not interested in pairwise differences. So A_E should be the same as E_A in this simple example.

Is there a clever short solution to paste them? I am currently thinking about sorting each one before combining them into a vector.

like image 322
Curlew Avatar asked Nov 23 '25 10:11

Curlew


1 Answers

One option is to use pmin and pmax:

transform(d, z = paste(pmin(x,y), pmax(x,y), sep="_"))
#  x y   z
#1 A E A_E
#2 B D B_D
#3 C C C_C
#4 D B B_D
#5 E A A_E

Note that you might need to convert x and y to character if they are factors.


d <- data.frame( x = LETTERS[1:5], y = LETTERS[5:1], stringsAsFactors = FALSE)
like image 56
talat Avatar answered Nov 26 '25 03:11

talat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!