Translate a vector based on a matrix on R -
i have indicate matrix, this:
john 1 ann 2 ruby 3 clair 4
so want translate vector number keep order, this:
(john,ann,john,clair,john,ruby,ann,john,ruby)->(1,2,1,4,1,3,2,1,3)
i don't know how r(without loop).
please me. thks
even in updated example, can use ?match
@pascal describes. don't match letters
.
# generate example df <- data.frame(input=c("john", "ann", "ruby", "clair"), output=1:4) x <- c("john", "ann", "john", "clair", "john", "ruby", "ann", "john", "ruby") # # if output indeed 1:nrow(df) match(x, df[, "input"]) # if output different df[match(x, df[, "input"]), "output"]
Comments
Post a Comment