[datatable-help] Normalize/average with grouping

Matthew Dowle mdowle at mdowle.plus.com
Mon Jul 16 11:08:59 CEST 2012


Hi,

If the two groupings are different, it'll need two groupings. But you
don't need the two setkeys so it can be one line instead of four :

> DT[,nvalue:=value/value[Tx=='Ctl'],by=EP][,mean(nvalue),by=Tx]
       Tx        V1
1:    Ctl 1.0000000
2:    Low 0.9273154
3: Medium 1.0596754
4:   High 1.2374553

Thanks for the nice example btw (above is tested).

Matthew

> 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
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
>




More information about the datatable-help mailing list