Search code examples
rset-difference

how may I remove elements in a string vector by elements in another string vector


setdiff(c("a","c"),c("a","b","c","d"))
 #character(0)

This code is supposed to return a vector of c("b","d"), what's wrong with it?


Solution

  • setdiff is asymmetric, as the help page warns about (though subtly).

    This works as you expect,

    > setdiff(c("a","b","c","d"),c("a","c"))
    [1] "b" "d"
    

    A simple function works either way,

    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"