I am trying to deal with some very messy data. I need to merge two large data frames which contain different kinds of data by the sample ID. The problem is that one table's sample IDs are in many different formats, but most contain the required ID string for matching somewhere in their ID, e.g. sample "1234" in one table has got an ID of "ProjectB(1234)" in the other.
I have made a minimal reproducible example.
a<-data.frame(aID=c("1234","4567","6789","3645"),aInfo=c("blue","green","goldenrod","cerulean"))
b<-data.frame(bID=c("4567","(1234)","6789","23645","63528973"), bInfo=c("apple","banana","kiwi","pomegranate","lychee"))
using merge gets part of the way:
merge(a,b, by.x="aID", by.y="bID", all=TRUE)
aID aInfo bInfo
1 1234 blue <NA>
2 3645 cerulean <NA>
3 4567 green apple
4 6789 goldenrod kiwi
5 (1234) <NA> banana
6 23645 <NA> pomegranate
7 63528973 <NA> lychee
but the output that would be liked is basically:
ID aInfo bInfo
1 1234 blue banana
2 3645 cerulean pomegranate
3 4567 green apple
4 6789 goldenrod kiwi
5 63528973 <NA> lychee
I just wondered if there was a way to incorporate grep into this or another R-tastic method?
Thanks in advance