Modifying Setdiff function in r

41 Views Asked by At

I am trying to find the difference between 2 tables based off one column but I am interested in knowing what is written in the whole row.

Table 1

enter image description here

Table 2

enter image description here

Currently I have used Setdiff(Table1$name, Table2$name) and it is returning "Susan" and "Frank". I am wondering if there is a way to return the whole row e.g. to show the other columns? Hence even though I am basing off the name, can the whole row with the name Susan show up? Thanks

1

There are 1 best solutions below

1
TimTeaFan On

With dplyr you could use anti_join to get the desired result:

Table1 <- data.frame(
  id  = 1:5,
  name = c("Cindy", "Sam", "Susan", "Frank", "Getty"),
  state = rep("OR", 5)
)

Table2 <- data.frame(
  id  = 1:5,
  name = c("Cindy", "Sam", "Tom", "John", "Getty"),
  state = c("OR", "OR", "TN", "TN", "OR")
)

library(dplyr)

Table1 |> 
  anti_join(Table2)

#> Joining with `by = join_by(id, name, state)`
#>   id  name state
#> 1  3 Susan    OR
#> 2  4 Frank    OR

Created on 2023-09-22 with reprex v2.0.2