I have two large dataframes. A minimum, reproducible example of them looks like this:
A <- data.frame(A=c("a","b","c","d"), B=c(1,2,3,4), C=c(1,2,NA,NA), D=c(1,2,3,4))
A
A B C D
1 a 1 1 1
2 b 2 2 2
3 c 3 NA 3
4 d 4 NA 4
B <- data.frame(A=c("c","d"), B=c(3,4), C=c(3,4))
B
A B C
1 c 3 3
2 d 4 4
For every row with a NA in A, I have a corresponding row in B with the replacement of the missing value. I would like to merge the two dataframes A and B to a "common" dataframe AB in a way that the NA's in dataframe A, column C are replaced by their corrsponding value in dataframe B, column C. The result should look like this:
AB <- data.frame(A=c("a","b","c","d"), B=c(1,2,3,4), C=c(1,2,3,4), D=c(1,2,3,4))
AB
A B C D
1 a 1 1 1
2 b 2 2 2
3 c 3 3 3
4 d 4 4 4
The "closest" (not so close either) I got to the solution was with the following code:
AB <- merge(A,B, all.x = TRUE)
AB
A B C D
1 a 1 1 1
2 b 2 2 2
3 c 3 NA 3
4 d 4 NA 4
Which, obviously, just uses the variables from A. I have already consulted the follwing questions:
- How to join (merge) data frames (inner, outer, left, right)
- Merging data frames with different number of rows and different columns
Please consider that the real dataframes are much larger. If you need any further information, please let me know. Thanks in advance!