[R] replace multiple values in vector at once
dwinsemius at comcast.net
Sat Jul 13 00:05:52 CEST 2013
On Jul 12, 2013, at 2:56 PM, Trevor Davies wrote:
> I always think that replying to your own r-help feels silly but it's good
> to close these things out.
> here's my hack solution:
That fairly tortured compared with:
x <- c(rep('x',3),rep('y',3),rep('z',3))
x1b <- as.character(1:3)[ match(x, c("x","y","z") ) ]
Furthermore, your solution does not deliver the answer you expected.
> Well that works and should for my more complex situation. If anyone has
> something a little less heavy handed I'd live to hear it.
> Have a great weekend.
> On Fri, Jul 12, 2013 at 2:18 PM, Trevor Davies <davies.trevor at gmail.com>wrote:
>> 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" "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).
>>  "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 :
>> 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.
> [[alternative HTML version deleted]]
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
Alameda, CA, USA
More information about the R-help