[R] Scaling a column in groups

john seers (IFR) john.seers at bbsrc.ac.uk
Fri Nov 16 15:20:44 CET 2007


 
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



More information about the R-help mailing list