Close, but not quite what I need.

That very nicely gives me sums by group.

I need to take each value of X and divide it by the sum of the group it belongs to.

With your example, I have 100,000 X and only 10,000 group.  The "by" command gives me 10,000 sums.  I still have to loop over all 100,000 entries, and find the group sum they belong to.


On Nov 29, 2012, at 11:05 AM, Mikołaj Hnatiuk <mikolaj.hnatiuk@gmail.com> wrote:

> Yes, type in:
> ?by
> 
> for example:
> data <- data.frame(fac=factor(c("A","A","B","B")), vec=c(1:4) )
> by(data$vec,data$fac, FUN=sum)
> 
> Best,
> Mikołaj Hnatiuk
> 
> 2012/11/29 Noah Silverman <noahsilverman@ucla.edu>
> 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@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.
> 


	[[alternative HTML version deleted]]


