[R] scaling-centering a vector using an index

Thomas de Cornulier oedic at cebc.cnrs.fr
Thu Sep 5 18:40:17 CEST 2002


All right, my question was unclear... hope this is better
I wanted to apply scale to each cell of a vector, that is to each group of
values given by a unique combination of the levels of certain factors. The
result should be a vector.
split and unsplit do what I need, but I don't understand why the first call
is the example below doesn't work as the second do:

x<-rnorm(10,3)
f1<-rep(c("a","b"),5)
f2<-rep(c("c","d"),c(5,5))
unsplit(lapply(split(x,list(f1,f2)),scale),list(f1,f2)) ##first call
group<-paste(f1,f2)
unsplit(lapply(split(x,group),scale),group) ##second call

thanks a lot to Peter Dalgaard BSA and J.R. Lockwood for their quick reply

thomas


Peter Dalgaard BSA wrote:

> Thomas de Cornulier <oedic at cebc.cnrs.fr> writes:
>
> > dear all,
> > I would like to center and scale some columns of a data frame (these
> > include NAs) according to the levels of an index (list of factors from
> > the same data frame).
> > I tried to code it using for loops but gave up. I guess there is a
> > straightforward way to achieve this: any hint welcome!
> > thanks a lot
> > thomas
>
> Erm, define "according to the levels of" or have a look at the
> examples for split/unsplit and see whether you mean what I think you
> mean....
>
> --
>    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