Replace values in a vector based on another vector

Solution 1:

Try with match

y[match(x, x.lvl)]

Solution 2:

Working with factors might be faster:

xf <- as.factor(x)
y[xf]

Note, that levels(xf) gives you a character vector similar to your x.lvl. Thus, for this method to work, elements of y should correspond to appropriate elements of levels(xf).