Say I have a vector with duplicate names:
x <- c(a=1, b=2, a=3, c=4, c=5, b=1, d=1)
I want to search and modify named elements. If I define
ElementsToChange <- c("a","b","c")
ChangeTo <- c(9,8,7)
I want to change all elements named "a" to 9 of all those named "b" by 8, etc., if I do this:
x[ElementsToChange] <- ChangeTo
This will change only the first (and not all) elements.
How can I change everything in a simple and elegant way?
source
share