I need to replace the values of a column model
in ddata
:
> unique(ddata$model)
[1] "GT-I9001" "iPhone5,2" "iPhone3,1"
[4] "iPhone4,1" "GT-I9300" "Nexus 4"
[7] "iPhone2,1" "VS840 4G" "HTC One X+"
...
with those from a lookup table, which I imported as
> devices
...
15 iPhone1,1 iPhone 2G,
16 iPhone1,2 iPhone 3G,
17 iPhone2,1 iPhone 3GS,
18 iPhone3,1 iPhone 4,
19 iPhone3,2 iPhone 4,
20 iPhone3,3 iPhone 4,
21 iPhone4,1 iPhone 4S,
22 iPhone5,1 iPhone 5,
23 iPhone5,2 iPhone 5,
...
So that, for example, iPhone4,1
becomes iPhone4S
. If a key is not present in the lookup table, I want to keep the original value of ddata$model
, so for example the original Nexus 4
remains Nexus 4
.
I tried with merge
, but I don't know how to address the case where a key is missing:
ddata2 = merge(ddata,devices,by="model", all.x=T)
The problem being that ddata2
has N/A for those values that don't have an entry in devices
.