Question: I have two data frames. I want to juxtopose them and choose only those rows that are in the second data frame.

Sample data:

```
d1 <- data.frame(id = c(1,222,3,44,56,7,8,9,10,12,13,14),
             vals = c(1,2,3,4,5,6,7,8,9,10,11,12))

d2 <- data.frame(id = c(222, 56, 7, 8, 12, 13, 14),
             sum = c(12, 13, 14, 15, 16, 16, 16))

```

Desirable output:

```
  d3 <- data.frame(id = c(222, 56, 7, 8, 12, 13, 14),
                  vals = c(2, 5, 6, 7, 10, 11, 12))

  d3
     id vals
  1 222    2
  2  56    5
  3   7    6
  4   8    7
  5  12   10
  6  13   11
  7  14   12

```

I tried using `%in%` operator, but it does not work properly - sometimes it takes ids that do not appear in d2.

P.S. Some context. This is a sample data and example; originally I have a summarised table (`summarise()`) with sums of absolute values from original data frame. From the summarised table I've deleted all sums that equal 0. I need all non-zero value rows remain in my, so to speak, original data frame, so, I am comparing my two tables.

Answer: 
Try this:

```
#Code
d3 <- d1[d1$id %in% d2$id,]

```

Output:

```
    id vals
2  222    2
5   56    5
6    7    6
7    8    7
10  12   10
11  13   11
12  14   12

```
You can use this:  

**Option 1**  

`d1[!d1$id%in%d2$id,]`  

**Option 2**  

`d1[is.na(match(d1$id,d2$id)),]`  

**Option 3**  

`library(dplyr) anti_join(d1, d2, by="id")`