I have two data frames.
DATA
Name Type Code
gabapentine Pharmaceutical 60142-96-3
Glyphosate Pesticide 1071-83-6
Guanylurea Pharmaceutical 141-83-3
hydrochlorthiazide Pharmaceutical 58-93-5
Values
Name Value Code
gabapentine 0,2 60142-96-3
Glyphosate 1,8 1071-83-6
Urea 1,2 141-83-3
hydrochlorthiazide 0,5 58-93-5
I want to add the column type
from Data
to Values
, by matching the columns Name
and Code
.
I know how to match with just one column, like this:
Values$type = Data$type[match(Values$Name, Data$Name)]
But now I want to take into account also the Code
, since some names don't match.
Is there a way to do it in just one line, like
Values$type = Data$type[match((Values$Name, Data$Name) | (Values$Code, Data$Code))]
That didn't work for me, so I would like to know the right way to do it.
I tried using merge like in other questions
merge(Values, Data,all.x = TRUE)
but in Guanylurea
from dataframe Data
I get type NA
when it should match Urea
from dataframe Values
. The result for that row would be Type
equal to Pharmaceutical
, but the Names
don't match exactly. So how can I add a partial match into functions match
or merge
? Or is there an alternative to these two?