I have two data frames: x contains two columns: A & B:
A B
4 1
6 2
9 2
10 3
15 3
data frame y contains several columns, of which two of them are of interest in my analysis:
C D
6 549
15 631
4 344
10 209
Note that column C in data frame y contains some (but not all) of the values available in column A of data frame x.
What I would like to do is to find the rows that there is match between x$A and y$D, then add the corresponding value in column D to my data frame x. So the resulting x would look like:
A B D
4 1 344
6 2 549
9 2 NA
10 3 209
15 3 631
You can specify the common columns to merge, which is what must be done here.
merge(x, y, by.x='A', by.y='C', all=TRUE)
A B D
1 4 1 344
2 6 2 549
3 9 2 NA
4 10 3 209
5 15 3 631
By default, merge uses the intersection of the column names to specify the common columns between the two data frames. When there are no column names in common, or when this is the incorrect set, you must specify with by or by.x and by.y.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With