独立行,考虑两列,在R中,没有顺序 [英] Unique rows, considering two columns, in R, without order

查看:162
本文介绍了独立行,考虑两列,在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行是重复,这意味着ba与ba相同。

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天全站免登陆