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.

有帮助吗?

解决方案 2

I would use merge like above, then follow with:

ddata2 <- within( ddata2, model[ is.na(model2) ] <- model[ is.na(model2) ] )

Assuming that the 2nd column in devices is called model2.

其他提示

Here's one approach using qdap:

## key <- readLines(n=9)
## iPhone1,1         iPhone 2G,
## iPhone1,2         iPhone 3G,
## iPhone2,1         iPhone 3GS,
## iPhone3,1         iPhone 4,
## iPhone3,2         iPhone 4,
## iPhone3,3         iPhone 4,
## iPhone4,1         iPhone 4S,
## iPhone5,1         iPhone 5,
## iPhone5,2         iPhone 5,
## 
## library(qdap)
## key <- colSplit(key, "         ")
## 
## ddata <- data.frame(model = c("GT-I9001",    "iPhone5,2",    "iPhone3,1",                        
##     "iPhone4,1",   "GT-I9300",     "Nexus 4",                         
##     "iPhone2,1",   "VS840 4G",     "HTC One X+"))

library(qdap)

ddata$model <- as.character(ddata$model)

ddata$model <- lookup(ddata$model, key, missing =NULL) ## OR
ddata$model <- ddata$model %l+% key

Please note that your data was difficult to read in. Please use dput for odd data sets. Also I believe a similar question was asked recently but can't find it.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top