本文介绍了无论位置如何,都能找到独特的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我确定这很简单,但是我有一个数据框
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更多推荐
无论位置如何,都能找到独特的组合
发布评论