[R] A More efficient method?

Stefan Grosse singularitaet at gmx.net
Wed Jul 4 17:16:51 CEST 2007


> 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"
>
>   
how about:

Cat<-c('a','a','a','b','b','b','a','a','b')
c1<- -2*(Cat=="a")+1



-=-=-
... Time is an illusion, lunchtime doubly so. (Ford Prefect)



More information about the R-help mailing list