[R] Fast ave for sorted data?

Zhou Fang zhou.zfang at gmail.com
Sun Feb 15 18:46:10 CET 2009


This is probably really obvious, by I can't seem to find anything on it.

Is there a fast version of ave for when the data is already sorted in 
terms of the factor, or if the breaks are already known?

Basically, I have:
X = 0.1, 0.2, 0.32, 0.32, 0.4, 0.56, 0.56, 0.7...
Y = 223, 434, 343, 544, 231.... etc
of the same, admittedly large length.

Now note that some of the values of X are repeated. What I want to do 
is, for those X that are repeated, take the corresponding values of Y 
and change them to the average for that particular X.

So, ave(Y,X) will work. But it's very slow, and certainly not suited to 
my problem, where Y changes and X stays the same and I need to 
repeatedly recalculate the averaging of Y. Ave also does not take take 
advantage of the sorting of the data.

So, is there an alternative? (Presumeably avoiding loops.)


Zhou Fang

More information about the R-help mailing list