无论位置如何,都能找到独特的组合 [英] Finding unique combinations irrespective of position

查看:90
本文介绍了无论位置如何,都能找到独特的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定这很简单,但是我有一个数据框

I'm sure it's something simple, but I have a data frame

      df <- data.frame(a = c(1, 2, 3),
                       b = c(2, 3, 1),
                       c = c(3, 1, 4))

我想要一个新的数据框,其中包含行中值的唯一组合,而不管它们位于哪一列.因此,在上述情况下,我想要

And I want a new data frame that contains the unique combinations of values in the rows, irrespective of which column they're in. So in the case above I'd want

    a b c
    1 2 3
    3 1 4

我尝试过

    unique(df[c('a', 'b', 'c')])

但是它认为(1、2、3)与(2、3、1)是唯一的,我不希望这样.

but it sees (1, 2, 3) as unique from (2, 3, 1), which I don't want.

推荐答案

也许是这样的

indx <- !duplicated(t(apply(df, 1, sort))) # finds non - duplicates in sorted rows
df[indx, ] # selects only the non - duplicates according to that index
#   a b c
# 1 1 2 3
# 3 3 1 4

这篇关于无论位置如何,都能找到独特的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆