[R] A More efficient method?

Ken Knoblauch knoblauch at lyon.inserm.fr
Wed Jul 4 17:11:59 CEST 2007


Keith Alan Chamberlain <Keith.Chamberlain <at> Colorado.EDU> writes:
> Cat=c('a','a','a','b','b','b','a','a','b')	# Categorical variable
> C1=vector(length=length(Cat))	# New vector for numeric values

> 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"

 ifelse(Cat == "a", -1, 1)
[1] -1 -1 -1  1  1  1 -1 -1  1

HTH



More information about the R-help mailing list