R在数据帧中生成非重复对 [英] R Generate non repeating pairs in dataframe
本文介绍了R在数据帧中生成非重复对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以目的是通过距离比较每个ID与每个ID。
So the purpose is to compare each ID with each other ID by taking distances.
考虑以下数据框 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
基本上,我正在尝试实现所有组合,以便在属于每个ID的功能之间取得距离。
Basically I am trying to achieve all combinations in order to take distances between the features belonging to each ID.
这里我真的不现在怎么开始任何洞察力RI的哪些工具可以使用?
Here I really do not now how to begin. Any insight? Which tools from R I could use?
推荐答案
使用combn和match的一个可能的解决方案。
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
这篇关于R在数据帧中生成非重复对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文