简体   繁体   中英

R: Currently unsupported data type when using period.apply

First a reproducible example:

library(quantstrat)

getSymbols("AAPL")

Test<-period.apply(AAPL,endpoints(AAPL,on="weeks",k=10),ROC)
TestDF<-as.data.frame(Test)

I want to get the ROC for a certain stock or whatever for x weeks. Or in other words, I want to compare several stocks and rank them with their 10-week ROC, 20 week ROC etc. Obviously the period apply works, however when I want to convert it to a data Frame and look at my data I always get this error:

Error in coredata.xts(x) : currently unsupported data type

Any idea whats wrong?

period.apply requires a function that returns a single row. ROC does not return a single row. So define your own function to do that.

Test <- period.apply(AAPL, endpoints(AAPL,on="weeks",k=10),
  function(x) log(last(x)/coredata(first(x))))

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