Comparing two similar data frames and finding different values ​​between them

This is apparently the main question, I apologize in advance if this is a duplicate question. I looked around and saw nothing.

I have two data frames full of rows. I would like to see if they are exact duplicates of each other.

If this is not the case, I would like to determine which values ​​are different.

In particular, given this data frame:

| x | y |
|---|---|
| a | e |
| b | f |
| c | g |
| d | h |

and this data file:

| x | y |
|---|---|
| a | l |
| b | m |
| j | g |
| k | h |

I would like to generate this result (df, full of inappropriate values):

| x | y |
|---|---|
|   | l |
|   | m |
| j |   |
| k |   |

This question is very close to what I think, but he wants to find complete strings that are the same, not the values.

1) , , , , . , df1 %in% df2 . ?

2) , , . , .

, , , . .

:

df1 <- data.frame(
  x = c('a', 'b', 'c', 'd'),
  y = c('e', 'f', 'g', 'h')
)


df2 <- data.frame(
  x = c('a', 'b', 'j', 'k'),
  y = c('l', 'm', 'g', 'h')
)
+4
1

:

df2[mapply(function(x,y)   x%in%y ,df1,df2)]<-NA
     x    y
1 <NA>    l
2 <NA>    m
3    j <NA>
4    k <NA>

df2, .

:
mapply() , %in% df1 df2, .., .
:

> mapply(function(x,y)   x%in%y,df1,df2)
         x     y
[1,]  TRUE FALSE
[2,]  TRUE FALSE
[3,] FALSE  TRUE
[4,] FALSE  TRUE

TRUE - , , , NA's.

+3

Source: https://habr.com/ru/post/1667179/


All Articles