Suppose I have data frame df1:
df1
R1
1 A
2 B
3 C
4 D
5 E
6 F
7 G
8 H
And another data frame df2:
df2
R1 R2
1 A 2
2 B 5
3 D 7
4 E 9
5 F 12
6 J 16
How would I make a new column in df1, named R2, that would ascribe the right value from df2$R2 to df1$R2 according to the matching entry from df2$R1? Any way I try to do this I end up getting errors related to the difference in lengths. However, is there a way I can force the values from df2$R2 into a new column in df1 and just have NAs (or NaNs or whatever) in any rows in df1 where no corresponding value exists in df2$R1? And also have it ignore entries in df2 for which there is no corresponding row in df1 (for example, row 6, where R1=J, because there is no J in df1$R1). For my example, the data set I want would look like this:
R1 R2
1 A 2
2 B 5
3 C NA
4 D 7
5 E 9
6 F 12
7 G NA
8 H NA
So basically, df1$R2 should equal what df2$R2 equals if df2$R1 is the same as df1$R1. Sorry if this has been asked before, I couldn't find it if it has. Thanks.