Saul Garcia
Saul Garcia

Reputation: 940

R Generate non repeating pairs in dataframe

So the purpose is to compare each ID with each other ID by taking distances.

Consider the following dataframe Df

ID AN     AW
a  white  green
b  black  yellow
c  purple gray
d  white  gray

In order to compare I need a combination looking like the following:

ID   AN     AW    ID2   AN2    AW2
a  white  green   b   black  yellow
a  white  green   c   purple gray
a  white  green   d   white  gray
b   black  yellow c   purple gray 
b   black  yellow d   white  gray
c   purple gray   d   white  gray

Basically I am trying to achieve all combinations in order to take distances between the features belonging to each ID.

Here I really do not now how to begin. Any insight? Which tools from R I could use?

Upvotes: 1

Views: 124

Answers (1)

Raad
Raad

Reputation: 2715

One possible solution using combn and match.

ids <- combn(unique(df$ID), 2)
data.frame(df[match(ids[1,], df$ID), ], df[match(ids[2,], df$ID), ])

#     ID     AN     AW ID.1   AN.1   AW.1
# 1    a  white  green    b  black yellow
# 1.1  a  white  green    c purple   gray
# 1.2  a  white  green    d  white   gray
# 2    b  black yellow    c purple   gray
# 2.1  b  black yellow    d  white   gray
# 3    c purple   gray    d  white   gray

Upvotes: 4

Related Questions