[R] Re: coding factor replicates

Warnes, Gregory R gregory_r_warnes at groton.pfizer.com
Thu Jan 24 03:19:56 CET 2002


How about this.  Its not as 'swish' as Doug's response, but it might be a
little simpler to think about and adapt...


# show the test vector
> tmp
 [1] A B C B C A C B A A B
Levels:  A B C 
>
# create a named vector to store the number of occurences of each level
> index <- numeric(length=length(levels(tmp)))
> names(index) <- levels(tmp)
> index
    A B C
[1] 0 0 0
>
# create a vector to store the results
> retval <- numeric(length(tmp))
>
# iterate across the vector, counting the number of occurences 
# store the cumulative count in the index vector and the current count in
the result vector
> for( i in 1:length(tmp) )
+    retval[i] <- index[tmp[i]] <- index[tmp[i]] + 1
>
# show the results
> retval
 [1] 1 1 1 2 2 2 3 3 3 4 4

-Greg


> -----Original Message-----
> From: Brad Buchsbaum [mailto:brad at aris.ss.uci.edu]
> Sent: Wednesday, January 23, 2002 4:52 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] Re: coding factor replicates
> 
> 
> 
> Hi All,
> 
> If I have a factor f:
> 
> A B C B C A C B A A B ....
> 
> and I would like to generate a factor to indicate the trial number
> as a function of condition: e.g.
> 
> 1 1 1 2 2 2 3 3 3 4 4 ...
> 
> how might I attack this in R?
> 
> thanks,
> 
> Brad Buchsbaum
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> -.-.-.-.-.-.-.-.-
> r-help mailing list -- Read 
> http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: 
> r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> _._._._._._._._._
> 


LEGAL NOTICE
Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list