[R] string/character to number

Søren Højsgaard Soren.Hojsgaard at agrsci.dk
Wed Jun 22 23:49:58 CEST 2005


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 at stat.math.ethz.ch på vegne af Jake Michaelson
Sendt: on 22-06-2005 23:35
Til: R-help at 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 at 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




More information about the R-help mailing list