Hi I have two dataframes A and B in R. I want to match the first column of A with the first or second column of dataframe B. Because the matched information is stored in the first or second column of dataframe B. Do you know how to do that? Thank you
Asked
Active
Viewed 348 times
2
-
2Please provide a reproducible example. – user438383 Apr 25 '22 at 16:32
-
First join B on A by the first column, then join B on A by the second column, then select the non NA value. If you provided a **minimal** reproducible example ([MRE](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610)) it would be possible to give more specific help. – dario Apr 25 '22 at 16:42
-
Please provide enough code so others can better understand or reproduce the problem. – Community Apr 26 '22 at 02:19
1 Answers
0
The sqldf
package has a function sqldf()
which allows you to run SQL statements on data frames as if they were database tables.
install.packages('sqldf')
library(sqldf)
C <- sqldf('select * from A join B on A.id in (B.id1, B.id2)')
It can work with inner joins (as above) or left joins. (Right joins and full outer joins are not supported at present.)

shaun_m
- 1,213
- 3
- 13