setdiff(c("a","c"),c("a","b","c","d"))
#character(0)
这段代码应该返回一个 c("b","d") 的向量,它有什么问题?
setdiff(c("a","c"),c("a","b","c","d"))
#character(0)
这段代码应该返回一个 c("b","d") 的向量,它有什么问题?
setdiff
是不对称的,正如帮助页面警告的那样(虽然很微妙)。
这可以按您的预期工作,
> setdiff(c("a","b","c","d"),c("a","c"))
[1] "b" "d"
无论哪种方式,一个简单的功能都可以工作,
setdiff2 <- function(x,y){
d1 <- setdiff(x,y)
d2 <- setdiff(y,x)
if(length(d2) > length(d1))
return(d2)
else
return(d1)
}
> setdiff2(c("a","c"), c("a","b","c","d"))
[1] "b" "d"