Skip to content
Back to formatted view

Raw Message

Message-ID: <50B7B39E.7030606@sapo.pt>
Date: 2012-11-29T19:12:30Z
From: Rui Barradas
Subject: Fast Normalize by Group
In-Reply-To: <A58D8CFA-A45B-4D74-9E7D-913F81A402C5@ucla.edu>

Hello,

If yopu want one value per group use tapply(), if you want one value per 
value of x use ave()

tapply(x, group, FUN = function(.x) .x/sum(.x))
ave(x, group, FUN = function(.x) .x/sum(.x))


Hope this helps,

Rui Barradas
Em 29-11-2012 18:55, Noah Silverman escreveu:
> 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?
>
> Thanks!
> ______________________________________________
> 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.