[R] A More efficient method?

joris.dewolf at cropdesign.com joris.dewolf at cropdesign.com
Wed Jul 4 17:30:58 CEST 2007



or
Cat <- c('a','a','a','b','b','b','a','a','b')
C1 <- (Cat=='a')*1







                                                                           
             "ONKELINX,                                                    
             Thierry"                                                      
             <Thierry.ONKELINX                                          To 
             @inbo.be>                 "Keith Alan Chamberlain"            
             Sent by:                  <Keith.Chamberlain at Colorado.EDU>,   
             r-help-bounces at st         <r-help at stat.math.ethz.ch>          
             at.math.ethz.ch                                            cc 
                                                                           
                                                                   Subject 
             04/07/2007 17:17          Re: [R] A More efficient method?    
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           
                                                                           




Cat <- c('a','a','a','b','b','b','a','a','b')
C1 <- ifelse(Cat == 'a', -1, 1)

------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be
www.inbo.be

Do not put your faith in what statistics say until you have carefully
considered what they do not say.  ~William W. Watt
A statistical analysis, properly conducted, is a delicate dissection of
uncertainties, a surgery of suppositions. ~M.J.Moroney



> -----Oorspronkelijk bericht-----
> Van: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] Namens Keith Alan
> Chamberlain
> Verzonden: woensdag 4 juli 2007 15:45
> Aan: r-help at stat.math.ethz.ch
> Onderwerp: [R] A More efficient method?
>
> 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"
>
> Sincerely,
> KeithC.
> Psych Undergrad, CU Boulder (US)
> RE McNair Scholar
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list