识别数据框 A 中不包含在数据框 B 中的记录
这是我第一次在这里发帖,所以请友善;-)
编辑 在我有机会做出建议的更改之前,我的问题已结束。所以我现在正在努力做得更好,感谢迄今为止回答的所有人!
问题
如何根据识别数据帧x.1
中未包含在数据帧x.2
中的记录/行所有可用的属性(即所有列)都以最有效的方式提供?
示例数据
> x.1 <- data.frame(a=c(1,2,3,4,5), b=c(1,2,3,4,5))
> x.1
a b
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
> x.2 <- data.frame(a=c(1,1,2,3,4), b=c(1,1,99,3,4))
> x.2
a b
1 1 1
2 1 1
3 2 99
4 3 3
5 4 4
所需结果
a b
2 2 2
5 5 5
迄今为止最佳解决方案,
由 Brian Ripley 教授和 Gabor Grothendieck 教授
> fun.12 <- function(x.1,x.2,...){
+ x.1p <- do.call("paste", x.1)
+ x.2p <- do.call("paste", x.2)
+ x.1[! x.1p %in% x.2p, ]
+ }
> fun.12(x.1,x.2)
a b
2 2 2
5 5 5
> sol.12 <- microbenchmark(fun.12(x.1,x.2))
> sol.12 <- median(sol.12$time)/1000000000
> sol.12
> [1] 0.000207784
迄今为止测试的所有解决方案的集合可在我的blog
最终编辑 2011-10-14
这是封装在函数“mergeX()”中的最佳解决方案:
setGeneric(
name="mergeX",
signature=c("src.1", "src.2"),
def=function(
src.1,
src.2,
...
){
standardGeneric("mergeX")
}
)
setMethod(
f="mergeX",
signature=signature(src.1="data.frame", src.2="data.frame"),
definition=function(
src.1,
src.2,
do.inverse=FALSE,
...
){
if(!do.inverse){
out <- merge(x=src.1, y=src.2, ...)
} else {
if("by.y" %in% names(list(...))){
src.2.0 <- src.2
src.2 <- src.1
src.1 <- src.2.0
}
src.1p <- do.call("paste", src.1)
src.2p <- do.call("paste", src.2)
out <- src.1[! src.1p %in% src.2p, ]
}
return(out)
}
)
This is my first time posting here, so please be kind ;-)
EDIT
My question was closed before I had a chance to make the changes suggested to me. So I'm trying to do a better job now, thanks for everyone that answered so far!
QUESTION
How can I identify records/rows in data frame x.1
that are not contained in data frame x.2
based on all attributes available (i.e. all columns) in the most efficient way?
EXAMPLE DATA
> x.1 <- data.frame(a=c(1,2,3,4,5), b=c(1,2,3,4,5))
> x.1
a b
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
> x.2 <- data.frame(a=c(1,1,2,3,4), b=c(1,1,99,3,4))
> x.2
a b
1 1 1
2 1 1
3 2 99
4 3 3
5 4 4
DESIRED RESULT
a b
2 2 2
5 5 5
BEST SOLUTION SO FAR
by Prof. Brian Ripley and Gabor Grothendieck
> fun.12 <- function(x.1,x.2,...){
+ x.1p <- do.call("paste", x.1)
+ x.2p <- do.call("paste", x.2)
+ x.1[! x.1p %in% x.2p, ]
+ }
> fun.12(x.1,x.2)
a b
2 2 2
5 5 5
> sol.12 <- microbenchmark(fun.12(x.1,x.2))
> sol.12 <- median(sol.12$time)/1000000000
> sol.12
> [1] 0.000207784
A collection of all solutions tested so far is available at my blog
FINAL EDIT 2011-10-14
Here's the best solution wrapped into a function 'mergeX()':
setGeneric(
name="mergeX",
signature=c("src.1", "src.2"),
def=function(
src.1,
src.2,
...
){
standardGeneric("mergeX")
}
)
setMethod(
f="mergeX",
signature=signature(src.1="data.frame", src.2="data.frame"),
definition=function(
src.1,
src.2,
do.inverse=FALSE,
...
){
if(!do.inverse){
out <- merge(x=src.1, y=src.2, ...)
} else {
if("by.y" %in% names(list(...))){
src.2.0 <- src.2
src.2 <- src.1
src.1 <- src.2.0
}
src.1p <- do.call("paste", src.1)
src.2p <- do.call("paste", src.2)
out <- src.1[! src.1p %in% src.2p, ]
}
return(out)
}
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有一些方法。 #1 和 #4 假设
x.1
的行是唯一的。 (如果x.1
的行不唯一,那么它们将仅返回重复行中的重复行之一。)其他行返回所有重复项:编辑:x.1 和 x.2 已交换并且这已被修复。还更正了开头的限制说明。
Here are a few ways. #1 and #4 assume that the rows of
x.1
are unique. (If rows ofx.1
are not unique then they will return only one of the duplicates among the duplicated rows.) The others return all duplicates:EDIT: x.1 and x.2 were swapped and this has been fixed. Also have corrected note on limitations at the beginning.
怎么样使用
merge
- 最简单的解决方案 - 我认为它也是最快的。对应于:(
您也可以使用 sqldf 来做到这一点)。
请注意,添加
myid
列是为了确保某些列没有 NA 值。如果您确定已经有一些列不包含 NULL 值,您可以简化解决方案:What about using
merge
- the simplest possible solution - I'd think it's also the fastest.Corresponds to:
(you can also use sqldf to do that).
Note that the column
myid
is added to assure that there is some column w/o NA values. If you are sure there is already some column which doesn't contain NULL values, you can simplify the solution: