Re: [R] string/character to number

From: Liaw, Andy <andy_liaw_at_merck.com>
Date: Thu 23 Jun 2005 - 07:58:51 EST


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 Received on Thu Jun 23 08:05:02 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:57 EST