Try to search your question here, if you can't find : Ask Any Question Now ?

What is the best way to apply a function to a column in a data frame with sequential changes in the function argument?

HomeCategory: stackoverflowWhat is the best way to apply a function to a column in a data frame with sequential changes in the function argument?
Avatarjohn asked 3 weeks ago

I am using quantmod in R to get price history for the ‘QQQ’ ETF. I want to calculate the 1:n period rate of change.

I am able to do it using ROC() and can successfully create and name the columns of the xts. I just see it as time intensive – I know there is a better way to do this.

library(quantmod)
getSymbols("QQQ")

QQQ <- QQQ$QQQ.Close
View(QQQ)
QQQ <- `colnames<-`(QQQ,"close")

QQQ$ROC1 <- ROC(x = QQQ[,1],n = 1)
QQQ$ROC2 <- ROC(x = QQQ[,1], n = 2)
QQQ$ROC3 <- ROC(x = QQQ[,1], n = 3)

I expect that I will have ‘n’ number of columns 1:n with respective ROC calculations. I understand there will be ‘n’ number of N/A values per column.

1 Answers
Best Answer
AvatarFernando answered 3 weeks ago
Your Answer

3 + 8 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com