如何识别“相似"? R中的行? [英] How to identify "similar" rows in R?

查看:101
本文介绍了如何识别“相似"? R中的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,社区的所有成员, 我正在尝试找出如何比较数据库元素并通过新的二进制变量来标识它们的方法. 我的数据库是这样的:

Hi to all the members of the community, I am trying to find out how to compare elements of my DB and identify them by a new binary variable. My DB is like this:

id=rep((1:2),5)
date<-seq(from=as.Date("2013-01-1"),to=as.Date("2013-01-05"),by=1)
trap<-c(1,1,3,1,4,2,3,4,1,4)
DB<-data.frame(id,date,trap)
DB<-DB[order(DB$date),]
DB$id[2]<-1
DB$trap[2]<-1
result<-c("N","N","N","N","N","N","Y","Y","Y","Y")
DB<-cbind(DB,result)

并且我想标识所有ID都不同的元素,但日期和陷阱值都与列结果中的报告相同.

and I want to identify all the elements for which the id is different, but the date and trap value is the same, as report in the column result.

我用ave函数尝试了一些代码(基本上是从另一个类似"问题中派生的),但是没有成功. 一如既往,任何提示将不胜感激!

I have tried some codes (basically derived from another "similar" question) with the ave function, but unsuccessfully. As always any tips will be appreciate!!

推荐答案

(duplicated(DB[,-1]) | duplicated(DB[,-1],fromLast=TRUE)) & 
         !(duplicated(DB) | duplicated(DB,fromLast=TRUE))
#[1] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE

这篇关于如何识别“相似"? R中的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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