且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在R中的数据框中查找重复的行(基于2列)

更新时间:2022-11-13 18:16:29

您可以随时尝试将前两列传递给函数复制

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

duplicated(dat[,1:2])

有关更多信息,请通过在控制台键入?复制,查看复制的函数的帮助文件。这将提供以下句子:

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.

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

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

,或者您可以跳过单独的分配步骤,只需使用:

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

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