# R Comparing Two Data Frames without using Merge

Stack Overflow Asked by RandomThinker on August 19, 2020

I want to compare two data frames with same structure of columns (in terms of number of columns and column names). If the value in a cell of the column A of the data frame X matches with the value in a cell of the column A of the data frame Y, the values in the cells (in the same row) of the column B and C of the data frame X will be changed to the values in the cells of the column B and C of the data frame Y. For example,

Data

df_X <- structure(list(A = c("Apple", "Banana", "Orange", "Banana"),
B = c(1L, 2L, 3L, 2L), C = c(2L, 2L, 3L, 1L), D = c(2L, 2L,
3L, 5L)), class = "data.frame", row.names = c(NA, -4L))

df_Y <- structure(list(A = c("Apple", "Banana."), B = c(4L, 2L), C = c(3L,
3L), D = c(2L, 4L)), class = "data.frame", row.names = c(NA,
-2L))

Data Frame X
A          B  C  D
Apple      1  2  2
Banana     2  2  2
Orange     3  3  3
Banana     2  1  5

Data Frame Y
A         B  C  D
Apple     4  3  2
Banana.   2  3  4


After Matching, data frame x will become:

Data Frame X
A          B  C  D
Apple      4  3  2
Banana     2  3  2
Orange     3  3  3
Banana     2  3  5


I know merging the two data frames cab probably do the trick. But I wonder can I achieve the task without using merge/left_join() (in dplyr)? Because my data frame is really long (lots of columns) and I want an independent data frame X in the end. Thanks!

You can use match to overwrite matching rows from x with y.

i <- match(x$A, y$A)
j <- !is.na(i)
x[j,c("B","C")] <- y[i[j],c("B","C")]
#       A B C D
#1  Apple 4 3 2
#2 Banana 2 3 2
#3 Orange 3 3 3
#4 Banana 2 3 5


Data:

x <- read.table(header=TRUE, text="
A          B  C  D
Apple      1  2  2
Banana     2  2  2
Orange     3  3  3
Banana     2  1  5")
A         B  C  D
Apple     4  3  2
Banana    2  3  4")


Correct answer by GKi on August 19, 2020

## Related Questions

### Keycloak permission to restrict account based resources

0  Asked on December 26, 2020 by james-lin

### CSS flex, full height sidebar inside a modal?

1  Asked on December 25, 2020 by ddulla

### Cant loop through List and display in DataTable

1  Asked on December 25, 2020 by finchy70

### How to fix Laravel Illegal mix of collations error?

1  Asked on December 25, 2020 by vince

### Automate and looping through batch script

2  Asked on December 25, 2020 by nck_505

### Ionic-5 app is closing instead back to previous page on click of hardware back button

2  Asked on December 25, 2020 by outofworld

### issue connecting Heroku PHP stack to Redis using Predis

0  Asked on December 25, 2020 by rob-edlin

### Angular.io v8: animate object and query()-children simple and together

2  Asked on December 24, 2020 by frank-nocke

### How to access a list’s length in a template’s code block for Django?

1  Asked on December 24, 2020 by nguyn-tng

### BLE Read operation not working in view model for Xamarin forms

0  Asked on December 24, 2020 by judson-abraham

### Ownership and inheritance within nested groups

1  Asked on December 24, 2020

### Splitting C linked list without making a copy

2  Asked on December 24, 2020

### Notice Messages Do Not Appear On The Page

1  Asked on December 24, 2020 by cifhai

### Making comparisons in a HTTPS GET request in node js

0  Asked on December 24, 2020 by thor_

### SwiftUI Horizontal ScrollView does not scroll

1  Asked on December 23, 2020 by hillinharwich

### removeEventListener error happen when building tic tac toe

3  Asked on December 23, 2020 by holly-wong

### Calling a tab fragment method inside the parent acitivity

0  Asked on December 23, 2020 by aaron-waller

### Hiding table rows works, but fading out does not

1  Asked on December 23, 2020 by diamondjoe12

### How to list kubernetes services in k9s?

1  Asked on December 23, 2020 by matthias-m

### redux-persist – how do you blacklist/whitelist nested state

2  Asked on December 23, 2020 by david-browning