Find elements not in smaller character vector list but in big list

前端 未结 3 1366
不知归路
不知归路 2020-12-09 21:21

I have the two big and small list. I want to know which of the elements in big list are not in smaller list. The list consists of property

([1] \"character\"         


        
3条回答
  •  没有蜡笔的小新
    2020-12-09 21:46

    ! only works on logical vectors. B is not logical, which is what causes the error. Decomposing the steps you're trying to make will show you this (i.e. !B). In this case, you want to use %in% (or match).

    A[!A %in% B]
    

    To decompose the above code:

    1. A %in% B creates a logical vector that is TRUE for values of A that exist in B.
    2. !A %in% B negates (reverses) the logic in (1)
    3. A[!A %in% B] returns the vector of elements that are TRUE in (2)

提交回复
热议问题