r - Is there a simpler way to translate numbers into strings? -
a vector in r
> <- c(1, 2, 3, 3, 2, 3, 1, 2, 1) that want "translate" (temporary) vector of strings 1 becomes "foo", 2 becomes "bar" , 3 becomes "baz".
i can achieve sapply:
> sapply(a, function(x) {if (x==1) return ('foo'); if (x==2) return ('bar'); return ('baz')}) [1] "foo" "bar" "baz" "baz" "bar" "baz" "foo" "bar" "foo" however, think there should alternative way without (what perceive misusing) sapply. case?
just try:
c("foo","bar","baz")[a] #[1] "foo" "bar" "baz" "baz" "bar" "baz" "foo" "bar" "foo"
Comments
Post a Comment