Compare if an observation it´s in another df

Hello

I have two dataframes df1 and df2

  df1 <- data.frame(id = c("1-1","1-1","1-1","1-1","1-1"),
                   group = c(1,1,1,2,2),
                   product = c(1,2,3,4,5),
                   client = c("90-1", "90-1","90-1","90-1","90-1"))
  
  df2 <- data.frame(id = c("1-1","1-1","1-1","1-1","1-1"),
                    group = c(1,1,1,2,2),
                    product = c(1,2,9,4,5),
                    client = c("90-1", "90-1","90-1","90-1","90-1"))


  > df2
     id group product client
  1 1-1     1       1   90-1
  2 1-1     1       2   90-1
  3 1-1     1       9   90-1
  4 1-1     2       4   90-1
  5 1-1     2       5   90-1
  > df1
     id group product client
  1 1-1     1       1   90-1
  2 1-1     1       2   90-1
  3 1-1     1       3   90-1
  4 1-1     2       4   90-1
  5 1-1     2       5   90-1
  > 

1. How Can you obtain a resut that shows the products mach or not as a TRUE and FALSE?
Or similar output:

  id group product client     new
  1 1-1     1       1   90-1  TRUE
  2 1-1     1       2   90-1  TRUE
  3 1-1     1       3   90-1  FALSE
  4 1-1     2       4   90-1  TRUE
  5 1-1     2       5   90-1  TRUE

2. How can you graph it?

Thank you very much!

Hi @bustosmiguel ,

library(dplyr)
result <- df1 %>%
  mutate(new = product %in% df2$product)

# id group product client   new
# 1 1-1     1       1   90-1  TRUE
# 2 1-1     1       2   90-1  TRUE
# 3 1-1     1       3   90-1 FALSE
# 4 1-1     2       4   90-1  TRUE
# 5 1-1     2       5   90-2  TRUE
1 Like

Hi,
thanks!!

  qplot(df1$product %in% df2$product)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.