[R] Re: coding factor replicates
David Brahm
brahm at alum.mit.edu
Thu Jan 24 16:54:05 CET 2002
Brad Buchsbaum <brad at aris.ss.uci.edu> has a factor:
R> f <- factor(c("A","B","C","B","C","A","C","B","A","A","B"))
and wants to create a vector (same length as f) to indicate trial number:
1 1 1 2 2 2 3 3 3 4 4
Here's a 2-line solution, done with matrices and cumsum():
R> m <- outer(f, unique(f), "==")
R> apply(m * apply(m, 2, cumsum), 1, sum)
[1] 1 1 1 2 2 2 3 3 3 4 4
With package "colSums" loaded, the second line could use rowSums() instead.
Is that "swish"?
--
-- David Brahm (brahm at alum.mit.edu)
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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