An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130712/adb81575/attachment.pl>
replace multiple values in vector at once
7 messages · David Winsemius, Trevor Davies, arun +2 more
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130712/945e69da/attachment.pl>
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 compared with:
x <- c(rep('x',3),rep('y',3),rep('z',3))
x1b <- as.character(1:3)[ match(x, c("x","y","z") ) ]
x1b
Furthermore, your solution does not deliver the answer you expected.
David.
>
> 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
>> [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 at 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.
David Winsemius
Alameda, CA, USA
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130712/74cbe17e/attachment.pl>
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 <davies.trevor at gmail.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc:
Sent: Friday, July 12, 2013 5:56 PM
Subject: Re: [R] replace multiple values in vector at once
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 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
[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 at 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.
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 3 Levels: 1 2 3 Cheers, Bert
On Fri, Jul 12, 2013 at 3:05 PM, David Winsemius <dwinsemius at comcast.net> wrote:
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 compared with:
x <- c(rep('x',3),rep('y',3),rep('z',3))
x1b <- as.character(1:3)[ match(x, c("x","y","z") ) ]
x1b
Furthermore, your solution does not deliver the answer you expected.
--
David.
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
[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 at 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.
David Winsemius Alameda, CA, USA
______________________________________________ R-help at 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.
Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
In the plyr package there are also the functions revalue and mapvalues:
library(plyr)
x <- c("a", "b", "c")
revalue(x, c(a = "A", c = "C"))
mapvalues(x, c("a", "c"), c("A", "C"))
mapvalues works on numeric, character and factor.
Jason
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Trevor Davies
Sent: Friday, July 12, 2013 2:57 PM
To: r-help at r-project.org
Subject: Re: [R] replace multiple values in vector at once
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 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
[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
______________________________________________ R-help at 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.