in R: find observations with unique combinations across columns, regardless their order [duplicate]

随声附和 提交于 2020-01-11 13:24:11

问题


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?


回答1:


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))),]


来源:https://stackoverflow.com/questions/35592803/in-r-find-observations-with-unique-combinations-across-columns-regardless-thei

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!