Replacing vector elements based on indices of another vector

Viewed 55

I have the following two vectors:

a<-c(1,4,6,5,5,2,4,6,1)
b<-c(0.5,1,1.5,2,2.5,3)

I want to replace each element in vector a with the element in b with the index given by the element in a. The resulting vector in this example should be:

c(0.5,2,3,2.5,2.5,1,2,3,0.5)
1 Answers

I guess you want this indexing

> b[a]
[1] 0.5 2.0 3.0 2.5 2.5 1.0 2.0 3.0 0.5
Related