如何删除所有重复项以使 NONE 留在数据框中? [英] How can I remove all duplicates so that NONE are left in a data frame?

查看:21
本文介绍了如何删除所有重复项以使 NONE 留在数据框中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个类似问题 对于 PHP,但我正在使用 R 并且无法将解决方案转换为我的问题.

There is a similar question for PHP, but I'm working with R and am unable to translate the solution to my problem.

我有这个包含 10 行和 50 列的数据框,其中一些行完全相同.如果我在它上面使用唯一的,我会得到一行 - 比如说 - 类型",但我真正想要的是只得到那些只出现一次的行.有谁知道我如何做到这一点?

I have this data frame with 10 rows and 50 columns, where some of the rows are absolutely identical. If I use unique on it, I get one row per - let's say - "type", but what I actually want is to get only those rows which only appear once. Does anyone know how I can achieve this?

我可以查看集群和热图以手动对其进行排序,但我的数据框比上面提到的更大(最多 100 行),这有点棘手.

I can have a look at clusters and heatmaps to sort it out manually, but I have bigger data frames than the one mentioned above (with up to 100 rows) where this gets a bit tricky.

推荐答案

这将提取只出现一次的行(假设您的数据框名为 df):

This will extract the rows which appear only once (assuming your data frame is named df):

df[!(duplicated(df) | duplicated(df, fromLast = TRUE)), ]

它是如何工作的:函数duplicated 测试一行是否从第一行开始至少第二次出现.如果使用参数 fromLast = TRUE,则函数从最后一行开始.

How it works: The function duplicated tests whether a line appears at least for the second time starting at line one. If the argument fromLast = TRUE is used, the function starts at the last line.

两个布尔结果与 |(逻辑或")组合成一个新向量,表示所有行多次出现.使用 ! 将结果取反,从而创建一个布尔向量,指示仅出现一次的行.

Boths boolean results are combined with | (logical 'or') into a new vector which indicates all lines appearing more than once. The result of this is negated using ! thereby creating a boolean vector indicating lines appearing only once.

这篇关于如何删除所有重复项以使 NONE 留在数据框中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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