Different Records

Asked

Viewed 94 times

3

I have two tables:

MATRICULA_A <-c(123,234,345,456)
dados_1 <- data.frame(MATRICULA_A)

MATRICULA_A <-c(345,456,111,222,333,444)
dados_2 <- data.frame(MATRICULA_A)

I need to extract only table information dados_1 that are different from dados_2. In this example, I want only license plates 123 and 234 to appear as a result (dados_1). I tried through the anti_join and did not give.

2 answers

5


I don’t know how you used the function anti_join, but she worked here:

library(dplyr)
anti_join(dados_1,dados_2,by='MATRICULA_A') # 'MATRICULA_A' é a variável em comum

  MATRICULA_A
1         123
2         234

2

On R base can do this in several ways.

With setdiff:

setdiff(dados_1$MATRICULA_A, dados_2$MATRICULA_A)
#[1] 123 234

With %in% is more flexible, can either obtain a vector or a class object "data.frame", just like the original df1:

i <- !dados_1$MATRICULA_A %in% dados_2$MATRICULA_A

dados_1[i, ]
#[1] 123 234

dados_1[i, , drop = FALSE]
#  MATRICULA_A
#1         123
#2         234

Browser other questions tagged

You are not signed in. Login or sign up in order to post.