[R] lookup not working properly

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Tue Apr 12 17:17:56 CEST 2011


Hello!

Below is my exmample. "myref" is my reference data frame with columns a and b.
"temp" is my data with column c analogous to column a in "myref".
I am trying to create a new variable b - in "temp" - that matches
values from b in "myref" to values in c. If you look at the resulting
data frame (temp - at the bottom), you'll notice that rows 19-24 are
incorrect.
How could one fix it?
Thanks a lot!

# my reference data frame:
a=c("ba ba","ca ca","da da", "lake lake, a", "lake lake, b","lake
of","lama ca, a","lama ca, b","ma ma")
b=c("ba ba","ca ca","OTHER", "lake lake, a", "lake lake, b","lake
of","lama ca, a","lama ca, b","OTHER")
myref<-data.frame(a=a, b=b)
(myref)

# my data:
c<-c(rep("ba ba",3),rep("ca ca",3),rep("da da",3),rep("lake lake, a",3),
  rep("lake lake, b",3),rep("lake of",3),rep("lama ca, a",3),rep("lama
ca ,b",3),rep("ma ma",3))
temp<-data.frame(c=c)
(temp)

### Matching:
temp$b<-myref[temp$c,"b"]
(temp)

-- 
Dimitri Liakhovitski
Ninah Consulting
www.ninah.com



More information about the R-help mailing list