I would like to match the elements of a column in a dataframe against another dataframe.
Consider these dataframes:
A=data.frame(par=c('long A story','long C story', 'blabla D'),val=1:3)
B=data.frame(par=c('Z','D','A'),val=letters[1:3])
Each element of B column ‘par’ should be matched against A column par.
If there is a match, it should be labeled in A.
[This then gives a column of common values for merging A and B].
The desired result is therefore:
A=transform(A,label=c('A','NA','D'))
How can this be done?
Henk
To do what you’re asking for, try
(I assumed you meant a capital A in your example
A=transform(a,label=c('A','NA','D')); in that case, these match exactly). EDIT: I see you made that edit. They do match then.The above method will work only if there is exactly one B that fits every A (in other words, there can be multiple As to a B but not multiple Bs to an A). This is because of the structure you want in the output.