-1

How am I able to subtract/match certain rows based on values of multiple columns?

So far, I've tried using %in% operator. However, this doesn't work and only gives me the logical values of columns. Here is an example:

> A
   x  y  z
1  1  1  A
2  2  1  B
3  3  2  C
4  4  3  D
5  4  3  E
6  5  4  F
7  5  4  G
8  6  5  H

> B
   x  y 
1  1  1
2  4  3
> A[c(1,2)] %in% B
FALSE FALSE 

But I need to get the logical value of each row like this:

1  TRUE
2  FALSE
3  FALSE
4  TRUE
5  TRUE
6  FALSE
7  FALSE
8  FALSE

In order to get the following:

> A[<some logical value>, ]
1  1  1  A
4  4  3  D
5  4  3  E
markus
  • 25,843
  • 5
  • 39
  • 58
Huy Huynh
  • 21
  • 1
  • 3

1 Answers1

0

what you need for your <some logical value> is A[,1] %in% B[,1] & A[,2] %in% B[,2]

> A[A[,1] %in% B[,1] & A[,2] %in% B[,2], ]
  x y z
1 1 1 A
4 4 3 D
5 4 3 E
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138