r - Replacing for-loops with apply to improve perfomance (with weighted.mean) -


i r newbie solvable problem of you. have dataframe containing more million data-points. goal compute weighted mean altering starting point.

to illustrate consider frame ( data.frame(matrix(c(1,2,3,2,2,1),3,2)) )

  x1 x2 1  1  2 2  2  2 3  3  1 

where x1 data , x2 sampling weight.

i want compute weighted mean x1 starting point 1 3, 2:3 , 3:3.

with loop wrote:

b <- rep(na,3) #empty result vector for(i in 1:3){   b[i] <- weighted.mean(x=a$x1[i:3],w=a$x2[i:3]) #shifting starting point of data , weights further end }  

with real data impossible compute because each iteration data.frame altered , computing takes hours no result.

is there way implement varrying starting point apply command, perfomance increases?

regards, ruben

building upon @joran's answer produce correct result:

with(a, rev(cumsum(rev(x1*x2)) / cumsum(rev(x2)))) # [1] 1.800000 2.333333 3.000000 

also note much faster sapply/lapply approach.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -