我有一个 data.table 如下 -
library(data.table)
library(quantmod)
dataTableTemp <- getSymbols('AAPL', auto.assign = FALSE)
dt = data.table("date" = index(dataTableTemp), coredata(dataTableTemp))
> dt
date AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
1: 2007-01-03 3.081786 3.092143 2.925000 2.992857 1238319600 2.581790
2: 2007-01-04 3.001786 3.069643 2.993571 3.059286 847260400 2.639095
3: 2007-01-05 3.063214 3.078571 3.014286 3.037500 834741600 2.620301
4: 2007-01-08 3.070000 3.090357 3.045714 3.052500 797106800 2.633241
5: 2007-01-09 3.087500 3.320714 3.041071 3.306071 3349298400 2.851985
---
3542: 2021-01-27 143.429993 144.300003 140.410004 142.059998 140843800 142.059998
3543: 2021-01-28 139.520004 141.990005 136.699997 137.089996 142621100 137.089996
3544: 2021-01-29 135.830002 136.740005 130.210007 131.960007 177180600 131.960007
3545: 2021-02-01 133.750000 135.380005 130.929993 134.139999 106239800 134.139999
3546: 2021-02-02 135.729996 136.309998 134.610001 134.990005 82919600 134.990005
到这个data.table,我想添加TTR::ADX
函数返回的几列
我尝试过类似以下的方法 -
dt[, c("DIp", "DIn", "DX", "ADX") := ADX(.SD, n = 14), .SDcols = c("AAPL.High", "AAPL.Low", "AAPL.Close")]
但是,我收到错误:
Error in `[.data.table`(x, , `:=`(c("DIp", "DIn", "DX", "ADX"), ADX(.SD, :
Supplied 14184 items to be assigned to 3546 items of column 'DIp'.
期待有关如何解决此问题的建议。
谢谢。