0
df1 
   id value
1  1    10
2  2    20
3  3    30
4  4    40
5  5    50

df2
   id value
1  2    25
2  3    35

I would like to have the following df3:

df3 
   id value
1  1    10
2  4    40
3  5    50

In other words, I would like to remove only the same ID between df1 and df2. How could I do that?

2 Answers2

1

I created new dummy data to simplify the example. Feel free to apply to other dataframes

# dummy df
df <- data.table(id = 1:2); df
   id
1:  1
2:  2

# dummy df1
df1 <- data.table(id = 2:3); df1
   id
1:  2
2:  3

# common rows
x <- intersect(unique(df$id), unique(df1$id))

# exclude
rbind(df, df1)[!id %in% x]
   id
1:  1
2:  3
Sweepy Dodo
  • 1,761
  • 9
  • 15
1

You can use

df1[!df1$id %in% df2$id, ]
#>   id value
#> 1  1    10
#> 4  4    40
#> 5  5    50

reproducible data from question

df1 <- data.frame(id = 1:5, value = 10 * (1:5))
df2 <- data.frame(id = 2:3, value = c(25, 35))
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • Hi, there. Neat solution. Tho I believe the difference btw ours is yours would not return rows from `df2` IF the entire `df2` is not a subset of `df1` Just had to point this out to readers, however, I admit this is not the case of OP's data – Sweepy Dodo Feb 06 '22 at 00:28