在 R 中的数据框中查找重复的行(基于 2 列) [英] Find duplicated rows (based on 2 columns) in Data Frame in R

查看:45
本文介绍了在 R 中的数据框中查找重复的行(基于 2 列)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 R 中有一个数据框,它看起来像:

I have a data frame in R which looks like:

| RIC    | Date                | Open   |
|--------|---------------------|--------|
| S1A.PA | 2011-06-30 20:00:00 | 23.7   |
| ABC.PA | 2011-07-03 20:00:00 | 24.31  |
| EFG.PA | 2011-07-04 20:00:00 | 24.495 |
| S1A.PA | 2011-07-05 20:00:00 | 24.23  |

我想知道关于 RIC 和 Date 的组合是否有任何重复.在 R 中有一个函数吗?

I want to know if there's any duplicates regarding to the combination of RIC and Date. Is there a function for that in R?

推荐答案

你总是可以尝试简单地将前两列传递给 duplicated 函数:

You can always try simply passing those first two columns to the function duplicated:

duplicated(dat[,1:2])

假设您的数据框名为 dat.有关更多信息,我们可以通过在控制台输入 ?duplicated 来查阅 duplicated 功能的帮助文件.这将提供以下句子:

assuming your data frame is called dat. For more information, we can consult the help files for the duplicated function by typing ?duplicated at the console. This will provide the following sentences:

确定向量或数据框的哪些元素是重复的具有较小下标的元素,并返回一个逻辑向量指示哪些元素(行)是重复的.

Determines which elements of a vector or data frame are duplicates of elements with smaller subscripts, and returns a logical vector indicating which elements (rows) are duplicates.

所以 duplicated 返回一个逻辑向量,然后我们可以使用它来提取 dat 的子集:

So duplicated returns a logical vector, which we can then use to extract a subset of dat:

ind <- duplicated(dat[,1:2])
dat[ind,]

或者您可以跳过单独的分配步骤并简单地使用:

or you can skip the separate assignment step and simply use:

dat[duplicated(dat[,1:2]),]

这篇关于在 R 中的数据框中查找重复的行(基于 2 列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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