[R] Help with vectors!

Frank Schwidom schwidom at gmx.net
Tue Sep 8 19:33:16 CEST 2015


On Sat, Sep 05, 2015 at 02:14:18PM -0700, Dan D wrote:
> # your data
> VAS<-c("Green","Green","Black","Green","White","Yellow","Yellow","Black","Green","Black")
> 
> # declare the new vector
> New_Vector<-numeric(length(VAS))
> 
> # brute force:
> New_Vector[VAS=="White"]<-1
> New_Vector[VAS=="Yellow"]<-2
> New_Vector[VAS=="Green"]<-3
> New_Vector[VAS=="Black"]<-4
> 
> # a little more subtle
> cols<-c("White","Yellow","Green","Black")
> for (i in 1:length(cols))  New_Vector[VAS==cols[i]]<-i
> 
> # and a general approach (that may give a different indexing, but can be
> used for any array)
> for (i in 1:length(unique(VAS))) New_Vector[VAS==unique(VAS)[i]]<-i
> cbind(1:length(unique(VAS)),unique(VAS)) # a decoding key for the color
> index
> 

# how about:

rank( VAS, ties.method='min')

Regards



More information about the R-help mailing list