无论位置如何,都能找到独特的组合

编程入门 行业动态 更新时间:2024-10-24 16:30:59
本文介绍了无论位置如何,都能找到独特的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我确定这很简单,但是我有一个数据框

I'm sure it's something simple, but I have a data frame

df <- data.frame(a = c(1, 2, 3), b = c(2, 3, 1), c = c(3, 1, 4))

我想要一个新的数据框,其中包含行中值的唯一组合,而不管它们位于哪一列.因此,在上述情况下,我想要

And I want a new data frame that contains the unique combinations of values in the rows, irrespective of which column they're in. So in the case above I'd want

a b c 1 2 3 3 1 4

我尝试过

unique(df[c('a', 'b', 'c')])

但是它认为(1、2、3)与(2、3、1)是唯一的,我不希望这样.

but it sees (1, 2, 3) as unique from (2, 3, 1), which I don't want.

推荐答案

也许是这样的

indx <- !duplicated(t(apply(df, 1, sort))) # finds non - duplicates in sorted rows df[indx, ] # selects only the non - duplicates according to that index # a b c # 1 1 2 3 # 3 3 1 4

更多推荐

无论位置如何,都能找到独特的组合

本文发布于:2023-11-29 22:59:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647882.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   都能   独特   位置

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!