R:在多列中查找模式 - 可能重复()?

Nan*_*Nan 2 r

我试图隔离数据框中具有共同值的条目:请参阅下面的内容以重建我的df的一部分:

Stand<-c("MY","MY","MY","MY","MY")
Plot<-c(12,12,12,12,12)
StumpNumber<-c(1,2,3,3,7)
TreeNumber<-c(1,2,3,4,8)
sample<-data.frame(Stand,Plot,StumpNumber,TreeNumber)
sample
Run Code Online (Sandbox Code Playgroud)

并获得一个输出,告诉我哪些条目具有共同的值.换句话说,为了给定的Stand,Plot,StumpNumber组合快速隔离存在多个TreeNumber(或多于一行)的情况.在示例代码中,StumpNumber 3具有TreeNumber 3和TreeNumber 4.

我对duplicated()的理解是可以找到在一列中出现重复值的实例 - 我该怎么做才能找到常见的列组合出现的情况?

谢谢.

Jos*_*ich 5

描述?duplicated表示它适用于data.frames行,而Details部分的第四段表示:

 The data frame method works by pasting together a character
 representation of the rows separated by ‘\r’, so may be imperfect
 if the data frame has characters with embedded carriage returns or
 columns which do not reliably map to characters.
Run Code Online (Sandbox Code Playgroud)

你怎么理解它只适用于单列?

假设TreeNumber是独一无二的范围内Stand,PlotStumpNumber你只需要从通话将它排除在外duplicated.

> duplicated(sample[,1:3])
[1] FALSE FALSE FALSE  TRUE FALSE
> duplicated(sample[,1:3], fromLast=TRUE)
[1] FALSE FALSE  TRUE FALSE FALSE
Run Code Online (Sandbox Code Playgroud)

更新 - 如果您想要所有重复的行,您可以执行以下操作:

> allDups <- duplicated(sample[,1:3],fromLast=TRUE) | duplicated(sample[,1:3])
> sample[allDups,]
  Stand Plot StumpNumber TreeNumber
3    MY   12           3          3
4    MY   12           3          4
Run Code Online (Sandbox Code Playgroud)