[R] string/character to number
I did a very quick search of the archive and couldn't find a readily available answer to this one: I'd like to convert, for example: c("a", "b", "a", "b") to c(1, -1, 1, -1) In the case of the first vector, it may be any length, but will always only have two unique values. It must always be replaced by corresponding values of 1 and -1. Any thoughts? Thanks in advance, Jake __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] string/character to number
Duh! sub() --Jake On Jun 22, 2005, at 3:35 PM, Jake Michaelson wrote: > I did a very quick search of the archive and couldn't find a readily > available answer to this one: > > I'd like to convert, for example: > > c("a", "b", "a", "b") > > to > > c(1, -1, 1, -1) > > In the case of the first vector, it may be any length, but will always > only have two unique values. It must always be replaced by > corresponding values of 1 and -1. > > Any thoughts? > > Thanks in advance, > > Jake > > __ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] string/character to number
This works x<-c("a", "b", "a", "b") x[x=="a"]<-1 x[x=="b"]<- -1 as.numeric(x) [1] 1 -1 1 -1 Fra: [EMAIL PROTECTED] på vegne af Jake Michaelson Sendt: on 22-06-2005 23:35 Til: R-help@stat.math.ethz.ch Emne: [R] string/character to number I did a very quick search of the archive and couldn't find a readily available answer to this one: I'd like to convert, for example: c("a", "b", "a", "b") to c(1, -1, 1, -1) In the case of the first vector, it may be any length, but will always only have two unique values. It must always be replaced by corresponding values of 1 and -1. Any thoughts? Thanks in advance, Jake __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] string/character to number
Note: sub() returns a character vector not a numeric vector. as.numeric() will convert it. Slightly slicker and faster is: 2*(z=='a')-1 where z is your vector, c('a','b','a','b') Cheers, Bert -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Jake Michaelson Sent: Wednesday, June 22, 2005 2:46 PM To: Jake Michaelson Cc: R-help@stat.math.ethz.ch Subject: Re: [R] string/character to number Duh! sub() --Jake On Jun 22, 2005, at 3:35 PM, Jake Michaelson wrote: > I did a very quick search of the archive and couldn't find a readily > available answer to this one: > > I'd like to convert, for example: > > c("a", "b", "a", "b") > > to > > c(1, -1, 1, -1) > > In the case of the first vector, it may be any length, but will always > only have two unique values. It must always be replaced by > corresponding values of 1 and -1. > > Any thoughts? > > Thanks in advance, > > Jake > > __ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] string/character to number
To do this in one step, it would be easier to use ifelse(): > Chars [1] "a" "b" "a" "b" > ifelse(Chars == "a", 1, -1) [1] 1 -1 1 -1 HTH, Marc Schwartz On Wed, 2005-06-22 at 15:46 -0600, Jake Michaelson wrote: > Duh! > > sub() > > --Jake > > On Jun 22, 2005, at 3:35 PM, Jake Michaelson wrote: > > > I did a very quick search of the archive and couldn't find a readily > > available answer to this one: > > > > I'd like to convert, for example: > > > > c("a", "b", "a", "b") > > > > to > > > > c(1, -1, 1, -1) > > > > In the case of the first vector, it may be any length, but will always > > only have two unique values. It must always be replaced by > > corresponding values of 1 and -1. > > > > Any thoughts? > > > > Thanks in advance, > > > > Jake > > __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] string/character to number
You can do indexing by name: > x <- c("a", "b", "a", "b") > v <- c(a=1, b=-1) > v[x] a b a b 1 -1 1 -1 Andy > From: Jake Michaelson > > Duh! > > sub() > > --Jake > > On Jun 22, 2005, at 3:35 PM, Jake Michaelson wrote: > > > I did a very quick search of the archive and couldn't find a readily > > available answer to this one: > > > > I'd like to convert, for example: > > > > c("a", "b", "a", "b") > > > > to > > > > c(1, -1, 1, -1) > > > > In the case of the first vector, it may be any length, but > will always > > only have two unique values. It must always be replaced by > > corresponding values of 1 and -1. > > > > Any thoughts? > > > > Thanks in advance, > > > > Jake > > > > __ > > R-help@stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! > > http://www.R-project.org/posting-guide.html > > > > __ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > > __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html