I got tableA
, which contains a numeric variable, CdTaller
(e.g. 1948675).
I also got tableB
, which contains two variables: Id
(e.g. 2513978) and Name
(e.g. "JOSE MANUEL PEREZ")
I want to add a new column to tableA
, Name_Taller
, which contains a name for those CdTaller
which correspond to the Id
variable of tableB
, so I do the following:
tableA[, Name_Taller := tableB[Id==tableA\$CdTaller]$Name]
However, for those CdTaller
of tableA
that are not in tableB
, R recycles the new variable Name_Taller
:
Warning message:
In \`[.data.table\`(tableA, , \`:=\`(Name, tableB[Id == :
Supplied 867324 items to be assigned to 947871 items of column 'Name'
(recycled leaving remainder of 80547 items).
So for the tableA$CdTaller == '1320402'
, which can't be found in tableB
, R still adds a Name_Taller
which actually corresponds to CdTaller == '4430219'
, because of this recycling.
The question is: how can I avoid this recycling, so that R only adds a NA
or empty string to those rows containing a CdTaller
not found in tableB
? Is there any parameter in data table I can use for this purpose?