# [R] A More efficient method?

François Pinard pinard at iro.umontreal.ca
Wed Jul 4 19:51:56 CEST 2007

```[Keith Alan Chamberlain]

>Is there a faster way than below to set a vector based on values
>from another vector? I'd like to call a pre-existing function for
>this, but one which can also handle an arbitrarily large number of
>categories. Any ideas?

>Cat=c('a','a','a','b','b','b','a','a','b')	# Categorical variable
>C1=vector(length=length(Cat))	# New vector for numeric values

># Cycle through each column and set C1 to corresponding value of Cat.
>for(i in 1:length(C1)){
>	if(Cat[i]=='a') C1[i]=-1 else C1[i]=1
>}

>C1
> -1 -1 -1  1  1  1 -1 -1  1
>Cat
> "a" "a" "a" "b" "b" "b" "a" "a" "b"

For handling an arbitrarily large number of categories, one may go
through a recoding vector, like this for the example above:

> Cat <- c('a', 'a', 'a', 'b', 'b', 'b', 'a', 'a', 'b')
> C1 <- c(a=-1, b=1)[Cat]
> C1
a  a  a  b  b  b  a  a  b
-1 -1 -1  1  1  1 -1 -1  1

--
François Pinard   http://pinard.progiciels-bpi.ca

```