I'm trying to find a function that can replace multiple instances of values
or characters in a vector in a one step operation.  As an example, the
vector:

x <- c(rep('x',3),rep('y',3),rep('z',3))

> x
[1] "x" "x" "x" "y" "y" "y" "z" "z" "z"

I would simply like to replace all of the x's with 1's, y:2 & z:3 (or other
characters).
i.e:
> x
[1] "1" "1" "1" "2" "2" "2" "3" "3" "3"

Of course, I'm aware of the replace function but this obviously gets a
little unwieldy when there are :
x<-replace(x,x=='x',1)
x<-replace(x,y=='x',2)
x<-replace(x,z=='x',3)

but I can't figure out how to do it in a one stop operation.  My real needs
is more complex obviously.  This is one of those seemingly simple
r-operations that should be obvious but I'm coming up empty on this one.

Thanks for the help.
Trevor

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to