分离任意(但不是全部)具有R的向量中的元素



如果你有一堆(字符,数字,无论什么)向量,什么是一个很好的方法来提取存在于一些/任何但不是所有这些向量的元素?

我试图找出我的表有共同的列名,想知道最聪明的方法是什么?

我想出了这个函数(见我的答案),但我想知道是否有更隐蔽的方法来做到这一点。

谢谢!

# take an arbitrary number of vectors of the same type
ana <-
    function( ... ){        
        w <- unique( unlist( list( ... ) ) )
        v <- Reduce( intersect , list( ... ) )
        setdiff( w , v )
    }
x <- 1:3
y <- 2:4
z <- 3:5
# return everything but the `3` since that's in all of the vectors.
ana( x , y , z )

最新更新