# [R] A More efficient method?

(Ted Harding) efh at nessie.mcc.ac.uk
Wed Jul 4 17:48:03 CEST 2007

```[Sorry, there were silly typose in the previous version. Corrected below]

On 04-Jul-07 13:44:44, Keith Alan Chamberlain wrote:
> Dear Rhelpers,
>
> 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  1
> Cat
> [1] "a" "a" "a" "b" "b" "b" "a" "a" "b"

> Cat=c('a','a','a','b','b','b','a','a','b')

> Cat=="b"
[1] FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE  TRUE

> (Cat=="b") - 0.5
[1] -0.5 -0.5 -0.5  0.5  0.5  0.5 -0.5 -0.5  0.5

> 2*((Cat=="b") - 0.5)
[1] -1 -1 -1  1  1  1 -1 -1  1

to give one example of a way to do it. But you don't say why you
really want to do this. You may really want factors. And what do
you want to see if there is "an arbitrarily large number of
categories"?

For instance:

> factor(Cat,labels=c(-1,1))
[1] -1 -1 -1 1  1  1  -1 -1 1

but this is not a vector, but a "factor" object. To get the vector,
you need to convert Cat to an integer:

> as.integer(factor(Cat))
[1] 1 1 1 2 2 2 1 1 2

where (unless you've specified otherwise in factor()) the values
will correspond to the elements of Cat in "natural" order, in this
case first "a" (-> 1), then "b" (-> 2).

E.g.

> Cat2<-c("a","a","c","b","a","b")
> as.integer(factor(Cat2))
[1] 1 1 3 2 1 2

so, with C2<-as.integer(factor(Cat2)), you get a vector of distinct
integers [1,2,3) for the distinct levels ("a","b","c") of Cat2.
If you want different integer values for these levels, you can write
a function to change them.

Hoping this helps to break the ice!
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <efh at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 04-Jul-07                                       Time: 16:44:20
------------------------------ XFMail ------------------------------

```