[R] Re: coding factor replicates
Peter Dalgaard BSA
p.dalgaard at biostat.ku.dk
Thu Jan 24 11:17:09 CET 2002
Douglas Bates <bates at stat.wisc.edu> writes:
> 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.
Essentially the same thing, but we saw a similar pattern recently and
I wonder whether we should systematize it:
"split<-" <- function(x,f,value){
x[unlist(split(seq(along=x),f)] <- unlist(value)
x
}
unsplit <- function(v,f) {
x <- vector(length=length(f))
split(x,f) <- v
x
}
Now we can do (breaks horribly if there's an empty factor level, though):
unsplit(lapply(table(f), seq)))
and the within-group z-score from the other day would become
unsplit(tapply(x,f,scale),f)
--
O__ ---- Peter Dalgaard Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics 2200 Cph. N
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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