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")
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
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP