Replace values in a vector based on another vector

2019-01-08 00:32发布

I would like to replace values in a vector (x) with values from another vector (y). Catch 22: The methods needs to be dynamic to accommodate different number of "levels" in vector x. For instance, consider vector x

x <- sample(c(1, 2, 3, 4, 5), 100, replace = TRUE)
> x
  [1] 2 4 1 1 3 1 1 1 1 1 2 2 5 5 4 5 5 3 4 1 2 2 3 3 3 5 1 3 4 5 5 3 2 4 3 1 3
 [38] 1 4 5 4 1 4 5 4 5 2 4 2 5 3 4 3 1 2 1 1 5 1 4 2 2 5 2 2 4 5 2 4 5 2 5 4 1
 [75] 3 3 4 4 1 1 4 4 2 4 5 4 5 5 4 2 5 2 4 5 3 2 1 1 2 2

where I would like to replace 1s with 100, 2s with 200 and so on.

This can be done easily with a for loop but for large vectors, several 100 thousand values, this is highly inefficient. Any tips how to optimize the code?

x <- sample(c(1, 2, 3, 4, 5), 100, replace = TRUE)
y <- c(100, 200, 300, 400, 500)
x.lvl <- c(1, 2, 3, 4, 5)
x.temp <- x

for (i in 1:length(y)) {
    x.temp[which(x == x.lvl[i])] <- y[i]
}

标签: r replace
2条回答
唯我独甜
2楼-- · 2019-01-08 01:08

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).

查看更多
对你真心纯属浪费
3楼-- · 2019-01-08 01:11

Try with match

y[match(x, x.lvl)]
查看更多
登录 后发表回答