vector - Replicate variable based off match of two other variables in R -
i've got seemingly simple question can't answer: i've got 3 vectors:
x <- c(1,2,3,4) weight <- c(5,6,7,8) y <- c(1,1,1,2,2,2)
i want create new vector replicates values of weight each time element in x matches y such produces following new weight vector associated y:
y_weight <- c(5,5,5,6,6,6)
any thoughts on how (either loop or vectorized)? thanks
you want match
function.
match(y, x)
to return indicies of matches, use build new weight vector
weight[match(y, x)]
Comments
Post a Comment