AnswerBun.com

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!

One Answer

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")
y <- read.table(header=TRUE, text="
A         B  C  D
Apple     4  3  2
Banana    2  3  4")

Correct answer by GKi on August 19, 2020

Add your own answers!

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

 

Automate and looping through batch script

2  Asked on December 25, 2020 by nck_505

       

issue connecting Heroku PHP stack to Redis using Predis

0  Asked on December 25, 2020 by rob-edlin

       

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

     

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

 

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP