[R] Scaling a column in groups

Greg Snow Greg.Snow at intermountainmail.org
Mon Nov 19 19:18:35 CET 2007


?ave

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at intermountainmail.org
(801) 408-8111
 
 

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of john seers (IFR)
> Sent: Friday, November 16, 2007 7:21 AM
> To: r-help at r-project.org
> Subject: [R] Scaling a column in groups
> 
> 
>  
> 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