R中的唯一行,考虑两列,无顺序 [英] Unique rows, considering two columns, in R, without order

查看:30
本文介绍了R中的唯一行,考虑两列,无顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与我发现的问题不同,我想获得两列的唯一性,没有顺序.

Unlike questions I've found, I want to get the unique of two columns without order.

我有一个df:

df<-cbind(c("a","b","c","b"),c("b","d","e","a"))
> df
     [,1] [,2]
 [1,] "a"  "b" 
 [2,] "b"  "d" 
 [3,] "c"  "e" 
 [4,] "b"  "a" 

在这种情况下,第 1 行和第 4 行是重复",因为 b-a 与 b-a 相同.

In this case, row 1 and row 4 are "duplicates" in the sense that b-a is the same as b-a.

我知道如何找到第 1 列和第 2 列的唯一性,但在这种方法下我会发现每一行都是唯一的.

I know how to find unique of columns 1 and 2 but I would find each row unique under this approach.

推荐答案

有很多方法可以做到这一点,这里有一个:

There are lot's of ways to do this, here is one:

unique(t(apply(df, 1, sort)))
duplicated(t(apply(df, 1, sort)))

一个给出唯一的行,另一个给出掩码.

One gives the unique rows, the other gives the mask.

这篇关于R中的唯一行,考虑两列,无顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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