Re: [R] string/character to number

From: Søren Højsgaard <Soren.Hojsgaard_at_agrsci.dk>
Date: Thu 23 Jun 2005 - 07:49:58 EST


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: r-help-bounces@stat.math.ethz.ch 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 Received on Thu Jun 23 07:54:51 2005

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