删除重复的组合(与顺序无关) [英] Removing duplicate combinations (irrespective of order)
问题描述
我有一个整数数据帧,它是所有n个子集的全部子集,选择1 ... n的3个组合。
例如,对于n = 5,它类似于:
I have a data frame of integers that is a subset of all of the n choose 3 combinations of 1...n. E.g., for n=5, it is something like:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 1 2 4
[3,] 1 2 5
[4,] 1 3 4
[5,] 1 3 5
[6,] 1 4 5
[7,] 2 1 3
[8,] 2 1 4
[9,] 2 1 5
[10,] 2 3 4
[11,] 2 3 5
[12,] 2 4 5
[13,] 3 1 2
[14,] 3 1 4
[15,] 3 1 5
[16,] 3 2 4
[17,] 3 2 5
[18,] 3 4 5
[19,] 4 1 2
[20,] 4 1 3
[21,] 4 1 5
[22,] 4 2 3
[23,] 4 2 5
[24,] 4 3 5
[25,] 5 1 2
[26,] 5 1 3
[27,] 5 1 4
[28,] 5 2 3
[29,] 5 2 4
[30,] 5 3 4
我想做的是删除具有重复组合的任何行,而不考虑顺序。例如, [1,] 1 2 3
与 [1,] 2 1 3
相同 [1,] 3 1 2
。
What I'd like to do is remove any rows with duplicate combinations, irrespective of ordering. E.g., [1,] 1 2 3
is the same as [1,] 2 1 3
is the same as [1,] 3 1 2
.
独特
,重复
和& c。似乎没有考虑到这一点。另外,我正在处理大量数据(n为〜750),因此它应该是一个相当快的操作。
unique
, duplicated
, &c. don't seem to take this into account. Also, I am working with quite a large amount of data (n is ~750), so it ought to be a pretty fast operation. Are there any base functions or packages that can do this?
推荐答案
只需在行内进行排序即可。像这样的东西:
Just sort within the rows first. Something like:
> dat = matrix(scan('data.txt'), ncol=3, byrow=T)
Read 90 items
> dat.sort = t(apply(dat, 1, sort))
> dat[!duplicated(dat.sort),]
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 1 2 4
[3,] 1 2 5
[4,] 1 3 4
[5,] 1 3 5
[6,] 1 4 5
[7,] 2 3 4
[8,] 2 3 5
[9,] 2 4 5
[10,] 3 4 5
这篇关于删除重复的组合(与顺序无关)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!