[datatable-help] Normalize/average with grouping

Sterkens, Patrick [JRDBE] PSTERKEN at its.jnj.com
Mon Jul 16 10:44:39 CEST 2012


Hi,
Data.table has been most helpful in the preparation of my data set thus
far; now I am just looking for an elegant way to do the last step.
Say I have 3 endpoints and 4 treatments:

DT=data.table(expand.grid(Tx=c("Ctl","Low","Medium","High"),EP=c("EP1","
EP2","EP3")))
set.seed(1)
DT[,value:=rnorm(12,mean=20,sd=5)]


I want to normalize the Tx (Low, Medium, High) w.r.t. the Ctl value in
each of the endpoints EP, and subsequently average the normalized values
across EP for the Tx.
My current approach is 

setkey(DT,EP)
DT[,nvalue:=value/value[Tx=='Ctl'],by=EP]
setkey(DT,Tx)
DT[,mean(nvalue),by=Tx]

1:    Ctl 1.0000000
2:    Low 0.9273154
3: Medium 1.0596754
4:   High 1.2374553

Is it possible to normalize and average in one go, even if the grouping
differs for each of the functions ?
Thanks
Patrick


More information about the datatable-help mailing list