Re: [R] Re: coding factor replicates

About this list Date view Thread view Subject view Author view Attachment view

From: Douglas Bates (bates@stat.wisc.edu)
Date: Thu 24 Jan 2002 - 09:47:27 EST


Message-id: <6r3d0w1y80.fsf@franz.stat.wisc.edu>

Uwe Ligges <ligges@statistik.uni-dortmund.de> writes:

> Brad Buchsbaum wrote:
> >
> > 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?
>
> What about something like
> as.factor(outer(rep(1, 3), 1:4))

I think the point is that the 1's are at the first occurence of the
level, the 2's at the second occurence, etc. This seems like the sort
of problem that Bill Venables would come up with a devilishly clever
way of solving.

I would do it as

> result <- seq(along = f) # create an vector to hold the response
> sp <- split(seq(along = f), f) # split the factor on levels
> result[unlist(sp)] <- unlist(lapply(sp, function(x) seq(along = x)))
> result
 [1] 1 1 1 2 2 2 3 3 3 4 4

but I'm sure Bill would do it much more elegantly than that.

-- 
Douglas Bates                            bates@stat.wisc.edu
Statistics Department                    608/262-2598
University of Wisconsin - Madison        http://www.stat.wisc.edu/~bates/
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


About this list Date view Thread view Subject view Author view Attachment view

This archive was generated by hypermail 2.1.3 : Wed 16 Oct 2002 - 11:56:55 EST