在R中:不管他们的顺序,找到具有独特组合的观察结果 [英] in R: find observations with unique combinations across columns, regardless their order

查看:121
本文介绍了在R中:不管他们的顺序,找到具有独特组合的观察结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含20个变量的1000个观察结果的数据框。



我只想选择只有具有独立组合的列,而不管其顺序如何。



也就是说,如果组合是 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屋!

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