Replace values that appear more than once in the vector in R

I have a value vector

x <- c(1,2,3,4,5)

For each occurrence, I want to replace it with the value in vector Y:

y <- c(8,9,11,24,56)

So when I test on a vector, such as

z <- (1,5,4)

I'm back now Z equals

z <- (8,56,24)

Solution

You can use function matching to find a match between Z and X and use them to Index Y:

y[match(z,x)]
# [1]  8  9 11 24 56 24 11 24  9 11  9 11 24
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>