I want to merge two data frames. So the column Type
from Data
appears in Values
.
DATA
Name Type Code
gabapentine Pharmaceutical 60142-96-3
Glyphosate Pesticide 1071-83-6
Guanylurea Pharmaceutical 141-83-3
hydrochlorthiazide Pharmaceutical 58-93-5
EDTA Industrial C. NA
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
EDTA 2,3 NA
I want to get this
Name Value Code Type
gabapentine 0,2 60142-96-3 Pharmaceutical
Glyphosate 1,8 1071-83-6 Pesticide
Guanylurea 1,2 141-83-3 Pharmaceutical
hydrochlorthiazide 0,5 58-93-5 Pharmaceutical
EDTA 2,3 NA Industrial C.
I tried the options of this question with merge
But I get
Name Value Code Type
gabapentine 0,2 60142-96-3 Pharmaceutical
Glyphosate 1,8 1071-83-6 Pesticide
Urea 1,2 141-83-3 NA
hydrochlorthiazide 0,5 58-93-5 Pharmaceutical
EDTA 2,3 NA Industrial C.
Is it possible to use the OR |
operator in the merge function? Or in the match function? Cause I want to match the Names
,and if they don't (as the case of Guanylurea and Urea), then look in the Codes
and finally add the Type
column.
Is possible something like this
Values$type = Data$type[match((Values$Name, Data$Name) | (Values$Code, Data$Code))]
or like this
merge(Data, Values, by=c("Name" | "Code")))