Find items not in the list of smaller characters, but in a large list

I have two large and small lists. I want to know which items from a large list are not in a smaller list. The list consists of a property

([1] "character"           "vector"              "data.frameRowLabels"
[4] "SuperClassMethod"

Here is a small example and the error I get

 A <- c("A", "B", "C", "D")
 B <- c("A", "B", "C")
  new <- A[!B]
Error in !B : invalid argument type

Expected Result - New <- c ("D")

+5
source share
3 answers

Take a look help("%in%")- here is an example at the bottom of this page that addresses this situation.

A <- c("A", "B", "C", "D")
B <- c("A", "B", "C")
(new <- A[which(!A %in% B)])

# [1] "D"

EDIT:

As Tyler points out, I have to take my own advice and read the support documents. which()not required when used %in%for this example. In this way,

(new <- A[!A %in% B])

# [1] "D"
+11
source

! . B , . , , (.. !B). %in% ( match).

A[!A %in% B]

:

  • A %in% B TRUE A B.
  • !A %in% B () (1)
  • A[!A %in% B] , TRUE (2)
+4

Although I think that setsmay help you sort out the different lists.

In your case, you can simply use:

A <- c("A", "B", "C", "D")
B <- c("A", "B", "C")

# to find difference
setdiff(A, B)

# to find intersect
intersect(A, B)

# to find union
union(A, B)
+1
source

All Articles