简体   繁体   中英

Rollapply for row in R with exp function

I have the data frame df(A) with 14 col and 1356 row. I have this function, for to apply a sum for every 12 row, like a moving average:

rollapply(A,width=12, FUN= sum,by.column=T) 

But inside every 12 row i need to apply a multiplication for exp(n/12) , where n are numbers from 0 to 11. In synthesis the results could be:

rollapply(A,FUN=1°row *exp(0/12)+2°row*exp(2/12)....+12°row*exp(11/12),by.column=T) 

Logically every 12 row the operation is repeated. Is possible apply this with function rollapply or i need to write a loop for ?

You just need to update your function:

n = 12
rollapply(A, width=n, FUN=function(x) sum(x*exp((seq(n)-1)/n)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM