Reputation: 117
I have 2 vectors:
Param<-c("alpha","beta","theta")
Trend<-c("b","c","ac","bc")
I want to replace every item of Trend vector with the items in Param vector, being "a" the first element (alpha), b the second (beta), and so on...
The desired result would be:
Result=("beta","theta","alphatheta","betatheta")
If possible i dont want to use any package, if not any idea is welcome :)
Upvotes: 1
Views: 30
Reputation: 887391
An option with str_replace
from stringr
which can take a named vector for replacement
library(stringr)
str_replace_all(Trend, set_names(Param, letters[1:3]))
#[1] "beta" "theta" "alphatheta" "betatheta"
Or if we dont' want to use any packages, use gsub
in a loop
lts <- letters[1:3]
for(i in seq_along(lts)) Trend <- gsub(lts[i], Param[i], Trend)
Trend
Upvotes: 0