[R] string/character to number

Marc Schwartz MSchwartz at mn.rr.com
Wed Jun 22 23:58:55 CEST 2005


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
> >




More information about the R-help mailing list