Re: [R] replace multiple values in vector at once

2013-07-12 Thread Law, Jason
s on numeric, character and factor. Jason -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Trevor Davies Sent: Friday, July 12, 2013 2:57 PM To: r-help@r-project.org Subject: Re: [R] replace multiple values in vector at once I always think t

Re: [R] replace multiple values in vector at once

2013-07-12 Thread Bert Gunter
David is right, but it's trivial if x is a factor (which is the default when you create character columns in a data frame). (Note also how to use rep() properly -- read the docs: ?rep) x <- factor(rep(LETTERS[1:3],e=3)) x [1] A A A B B B C C C Levels: A B C levels(x) <- 1:3 x [1] 1 1 1 2 2 2 3 3

Re: [R] replace multiple values in vector at once

2013-07-12 Thread arun
Hi, library(car)  recode(x,"'x'=1;'y'=2;'z'=3") #[1] 1 1 1 2 2 2 3 3 3 #or as.numeric(factor(x)) #[1] 1 1 1 2 2 2 3 3 3 A.K. - Original Message - From: Trevor Davies To: "r-help@r-project.org" Cc: Sent: Friday, July 12, 2013 5:56 PM

Re: [R] replace multiple values in vector at once

2013-07-12 Thread Trevor Davies
Yes, I caught my error once I posted it - I was fiddling with match prior to hammering down with merge but your solution is much better. Thank you. On Fri, Jul 12, 2013 at 3:05 PM, David Winsemius wrote: > > On Jul 12, 2013, at 2:56 PM, Trevor Davies wrote: > > > I always think that replying to

Re: [R] replace multiple values in vector at once

2013-07-12 Thread David Winsemius
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: > > x1<-merge(data.frame(A=x),data.frame(A=c('x','y','z'),B=c(1,2,2)),by='A')[,2] That fairly tortured compare

Re: [R] replace multiple values in vector at once

2013-07-12 Thread Trevor Davies
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: x1<-merge(data.frame(A=x),data.frame(A=c('x','y','z'),B=c(1,2,2)),by='A')[,2] Well that works and should for my more complex situation. If anyone has something a little

[R] replace multiple values in vector at once

2013-07-12 Thread Trevor Davies
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,