[R] Scaling a column in groups

john seers (IFR) john.seers at bbsrc.ac.uk
Fri Nov 16 16:27:43 CET 2007


 
Ah, that is neat.

Thanks.

JS



 -----Original Message-----
From: Gabor Grothendieck [mailto:ggrothendieck at gmail.com] 
Sent: 16 November 2007 15:12
To: john seers (IFR)
Cc: r-help at r-project.org
Subject: Re: [R] Scaling a column in groups

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