[R] Scaling a column in groups

Gabor Grothendieck ggrothendieck at gmail.com
Fri Nov 16 16:12:13 CET 2007


Try this:

c(apply(matrix(testdata, 25), 2, scale))

On Nov 16, 2007 9:20 AM, john seers (IFR) <john.seers at bbsrc.ac.uk> wrote:
>
> Hello R people
>
> I have a column of numeric values that are grouped in blocks of 25 and
> to be centered and scaled within each block. (That is subtract the mean
> and divide by the standard deviation.)
>
> Is there a neater way to do this? i.e not using a loop?
>
> Example looped code:
>
> testdata<-1:100
> csvalues<-NULL
> for (i in 1:(length(testdata)/25)) {
>    st<-(i-1) * 25 + 1
>    sel<-st:(st+24)
>    selvals<-testdata[sel]
>    csvalues<-c(csvalues, scale(selvals, scale=sd(selvals)))
> }
>
> Thanks for any help.
>
>
> John Seers
>
>
>
>
> ---
>
> John Seers
> Institute of Food Research
> Norwich Research Park
> Colney
> Norwich
> NR4 7UA
>
>
> tel +44 (0)1603 251497
> fax +44 (0)1603 507723
> e-mail john.seers at bbsrc.ac.uk
> e-disclaimer at http://www.ifr.ac.uk/edisclaimer/
>
> Web sites:
>
> www.ifr.ac.uk
> www.foodandhealthnetwork.com
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list