本文介绍了删除重复组合(不分顺序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个整数数据框,它是所有 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.

unique, duplicated, &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?

推荐答案

先在行内排序,然后使用重复,见下图:

# example data
dat = matrix(scan('data.txt'), ncol = 3, byrow = TRUE)
# Read 90 items

dat[ !duplicated(apply(dat, 1, sort), MARGIN = 2), ]
#       [,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

这篇关于删除重复组合(不分顺序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 16:48