[R] Fast Normalize by Group
Berend Hasselman
bhh at xs4all.nl
Thu Nov 29 20:19:00 CET 2012
On 29-11-2012, at 19:55, Noah Silverman wrote:
> Hi,
>
> I have a very large data set (aprox. 100,000 rows.)
>
> The data comes from around 10,000 "groups" with about 10 entered per group.
>
> The values are in one column, the group ID is an integer in the second column.
>
> I want to normalize the values by group:
>
> for(g in unique(groups){
> x[group==g] / sum(x[group==g])
> }
>
> This works find in a loop, but is slow. Is there a faster way to do this?
Toy example:
gx <- data.frame(group=rep(1:4,each=3), x=1:12)
gx
gx$x <- ave(gx$x, gx$group, FUN=function(x) x/sum(x))
gx
Berend
More information about the R-help
mailing list