在R中:不管他们的顺序,找到具有独特组合的观察结果 [英] in R: find observations with unique combinations across columns, regardless their order
问题描述
我只想选择只有具有独立组合的列,而不管其顺序如何。
也就是说,如果组合是 ABA
而另一个是 BAA
,我希望代码只返回这些组合之一。
为了识别独特的组合,我运行一个简单的唯一的
命令跨多个变量。
你如何编写这样的代码?
我们可以通过排序
c>应用与 MARGIN = 1
,然后使用重复的
返回逻辑索引,否定它,并获取数据中的唯一
行。
dat [ !重复(t(apply(dat,1,sort)))]]
I have a data frame with 1000 observations on 20 variables.
I want to select only the rows that have a unique combination across columns, regardless of their order.
That is, if a combination is ABA
and another is BAA
, I want the code only to return one of these combinations.
To identify unique combinations I run a simple unique
command across multiple variables.
How would you write such a code?
We can sort
the data by row using apply
with MARGIN=1
, then use duplicated
to return the logical index, negate it and get the unique
rows in the data.
dat[!duplicated(t(apply(dat, 1, sort))),]
这篇关于在R中:不管他们的顺序,找到具有独特组合的观察结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!