R lag在具有计数器变量的循环内不起作用?

时间:2018-05-09 04:21:20

标签: r lag

这在循环之外工作

> past=lag(zoo(c(new$SPCS20RSA)), c(-1,-2,-3,-4,-5), na.pad =TRUE)
> print(past)
       lag-1    lag-2    lag-3    lag-4    lag-5
1         NA       NA       NA       NA       NA
2   169.5526       NA       NA       NA       NA
3   169.5526 169.5526       NA       NA       NA

我想用新[i](或[a])

替换新的$ SPCS20RSA

如果我在试图利用计数器的循环中运行滞后,我会收到错误。 错误:

for (i in 1:10)
{
  #doesn't work in a loop
  past = lag(c(new[i]), c(-1,-2, -3, -4, -5), na.pad =TRUE)
  print(past)
} 
  

attr(x," tsp")< - value:' tsp'属性必须是数字   长度为3另外:警告信息:1:In if(k!=   round(k)){:条件长度> 1而且只有第一个   元素将被使用2:In(k / p [3L])* c(1,1,0):更长的对象   长度不是较短物体长度的倍数3:在p - (k / p [3L])   * c(1,1,0):较长的物体长度不是较短物体长度的倍数

如果我尝试[,i]

Error in new[, i] : incorrect number of dimensions

新请求的内容

> str(new)

List of 79
 $ date            : Date[1:516], format: "2008-01-01" "2008-04-01" "2008-05-04" ...
 $ CPIAUCSL        : num [1:516] 215 215 215 216 216 ...
 $ UNRATE          : num [1:516] 5.4 5.4 5.4 5.45 5.5 5.55 5.6 5.64 5.68 5.72 ...
 $ MEHOINUSA672N   : num [1:516] 56076 55979 55944 55936 55929 ...
 $ INTDSRUSM193N   : num [1:516] 2.25 2.25 2.25 2.25 2.25 2.25 2.25 2.25 2.25 2.25 ...
 $ CIVPART         : num [1:516] 66.1 66.1 66.1 66.1 66.1 66.1 66.1 66.1 66.1 66.1 ...
 $ LFWA64TTUSM647S : num [1:516] 1.96e+08 1.96e+08 1.96e+08 1.96e+08 1.96e+08 ...
 $ FEDFUNDS        : num [1:516] 1.98 1.98 1.98 1.98 1.99 ...
 $ GDPC1           : num [1:516] 14963 14963 14939 14933 14928 ...
 $ A191RL1Q225SBEA : num [1:516] 2 2 0.6594 0.375 0.0906 ...
 $ SP500           : num [1:516] 1412 1412 1412 1401 1413 ...
 $ DCOILWTICO      : num [1:516] 114 114 114 123 125 ...
 $ CSUSHPINSA      : num [1:516] 167 167 167 167 167 ...
 $ DFF             : num [1:516] 1.95 1.95 1.95 1.95 1.94 ...
 $ DFII10          : num [1:516] 1.52 1.52 1.52 1.48 1.44 ...
 $ A939RX0Q048SBEA : num [1:516] 49196 49196 49074 49048 49022 ...
 $ PCEPILFE        : num [1:516] 98.7 98.7 98.7 98.8 98.8 ...
 $ GDPDEF          : num [1:516] 99 99 99.2 99.3 99.3 ...
 $ SPCS20RSA       : num [1:516] 170 170 170 169 168 ...
 $ GDPPOT          : num [1:516] 15068 15068 15090 15094 15099 ...
 $ CPILFESL        : num [1:516] 215 215 215 215 215 ...
 $ GOLDAMGBD228NLBM: num [1:516] 859 859 859 878 875 ...
 $ CPIAUCNS        : num [1:516] 217 217 217 217 218 ...
 $ VIXCLS          : num [1:516] 18.5 18.5 18.5 19.1 17.2 ...
 $ WPU0911         : num [1:516] 171 171 171 171 172 ...
 $ PCEPI           : num [1:516] 100 100 100 100 100 ...
 $ USSTHPI         : num [1:516] 361 361 357 356 355 ...
 $ DSPIC96         : num [1:516] 11432 11432 11432 11356 11281 ...
 $ DCOILBRENTEU    : num [1:516] 110 110 110 120 123 ...
 $ FPCPITOTLZGUSA  : num [1:516] 3.84 2.8 2.43 2.35 2.27 ...
 $ PCEC96          : num [1:516] 10084 10084 10084 10081 10077 ...
 $ PPIACO          : num [1:516] 197 197 197 198 199 ...
 $ MEPAINUSA672N   : num [1:516] 29556 29477 29448 29442 29436 ...
 $ GDPCA           : num [1:516] 14830 14729 14692 14684 14676 ...
 $ MPRIME          : num [1:516] 5 5 5 5 5 5 5 5 5 5 ...
 $ PAYEMS          : num [1:516] 137870 137870 137870 137832 137793 ...
 $ CES0500000003   : num [1:516] 21.5 21.5 21.5 21.5 21.5 ...
 $ RECPROUSM156N   : num [1:516] 78.2 78.2 78.2 79.7 81.2 ...
 $ IC4WSA          : num [1:516] 363500 363500 363500 363750 368250 ...
 $ AHETPI          : num [1:516] 18 18 18 18 18 ...
 $ M2V             : num [1:516] 1.92 1.92 1.92 1.92 1.91 ...
 $ INDPRO          : num [1:516] 103 103 103 103 103 ...
 $ PCE             : num [1:516] 10093 10093 10093 10107 10121 ...
 $ UMCSENT         : num [1:516] 59.8 59.8 59.8 58.9 58.1 ...
 $ HDTGPDUSQ163N   : num [1:516] 97.7 97.7 98 98.1 98.1 ...
 $ M1V             : num [1:516] 10.6 10.6 10.5 10.5 10.5 ...
 $ TCU             : num [1:516] 79.6 79.6 79.6 79.6 79.5 ...
 $ STLFSI          : num [1:516] 0.99 0.99 0.99 0.757 0.653 0.585 0.667 0.743 0.889 0.886 ...
 $ BASE            : num [1:516] 857 857 857 857 859 ...
 $ PSAVERT         : num [1:516] 7.9 7.9 7.9 7.3 6.7 6.1 5.5 5.28 5.06 4.84 ...
 $ M2              : num [1:516] 7673 7673 7673 7673 7688 ...
 $ M1              : num [1:516] 1394 1394 1394 1394 1393 ...
 $ M1SL            : num [1:516] 1394 1394 1394 1396 1398 ...
 $ M2SL            : num [1:516] 7696 7696 7696 7701 7705 ...
 $ T10Y2Y          : num [1:516] 1.42 1.42 1.42 1.53 1.41 ...
 $ DGS10           : num [1:516] 3.83 3.83 3.83 3.85 3.86 ...
 $ BAMLH0A0HYM2    : num [1:516] 6.72 6.72 6.72 6.76 6.73 ...
 $ TB3MS           : num [1:516] 1.73 1.73 1.73 1.76 1.79 ...
 $ T10YIE          : num [1:516] 2.31 2.31 2.31 2.37 2.41 ...
 $ TEDRATE         : num [1:516] 1.33 1.33 1.33 1.09 0.91 ...
 $ GFDEGDQ188S     : num [1:516] 64.1 64.1 65.3 65.5 65.8 ...
 $ T5YIFR          : num [1:516] 2.33 2.33 2.33 2.43 2.48 ...
 $ T10Y3M          : num [1:516] 2.36 2.36 2.36 2.21 2.04 ...
 $ DGS1            : num [1:516] 1.96 1.96 1.96 1.94 2.07 ...
 $ USSLIND         : num [1:516] 0.02 0.02 0.02 -0.01 -0.04 -0.07 -0.1 -0.162 -0.224 -0.286 ...
 $ BAMLC0A4CBBB    : num [1:516] 2.98 2.98 2.98 2.96 2.94 ...
 $ GFDEBTN         : num [1:516] 9492006 9492006 9675128 9713972 9752816 ...
 $ DGS2            : num [1:516] 2.42 2.42 2.42 2.32 2.44 ...
 $ GS10            : num [1:516] 3.88 3.88 3.88 3.93 3.99 ...
 $ DGS5            : num [1:516] 3.12 3.12 3.12 3.07 3.12 ...
 $ DGS30           : num [1:516] 4.53 4.53 4.53 4.57 4.58 ...
 $ TREAST          : num [1:516] 536714 536714 536714 536714 515656 ...
 $ BAA10Y          : num [1:516] 3 3 3 3.04 3.06 ...
 $ BAMLC0A0CM      : num [1:516] 2.55 2.55 2.55 2.52 2.51 ...
 $ BAMLH0A3HYC     : num [1:516] 10.6 10.6 10.6 10.6 10.6 ...
 $ FYFSD           : num [1:516] -458553 -458553 -458553 -458553 -458553 ...
 $ DGS1MO          : num [1:516] 1.24 1.24 1.24 1.52 1.83 ...
 $ T5YIE           : num [1:516] 2.29 2.29 2.29 2.3 2.34 ...
 $ FutureSPCS20RSA : num 170

> new <- head(new)
> print(new)

$`date`
  [1] "2008-01-01" "2008-04-01" "2008-05-04" "2008-05-11" "2008-05-18" "2008-05-25" "2008-06-01" "2008-06-08"
  [9] "2008-06-15" "2008-06-22" "2008-06-29" "2008-07-06" "2008-07-13" "2008-07-20" "2008-07-27" "2008-08-03"
 [17] "2008-08-10" "2008-08-17" "2008-08-24" "2008-08-31" "2008-09-07" "2008-09-14" "2008-09-21" "2008-09-28"
 [25] "2008-10-05" "2008-10-12" "2008-10-19" "2008-10-26" "2008-11-02" "2008-11-09" "2008-11-16" "2008-11-23"
 [33] "2008-11-30" "2008-12-07" "2008-12-14" "2008-12-21" "2008-12-28" "2009-01-04" "2009-01-11" "2009-01-18"
 [41] "2009-01-25" "2009-02-01" "2009-02-08" "2009-02-15" "2009-02-22" "2009-03-01" "2009-03-08" "2009-03-15"
 [49] "2009-03-22" "2009-03-29" "2009-04-05" "2009-04-12" "2009-04-19" "2009-04-26" "2009-05-03" "2009-05-10"
 [57] "2009-05-17" "2009-05-24" "2009-05-31" "2009-06-07" "2009-06-14" "2009-06-21" "2009-06-28" "2009-07-05"
 [65] "2009-07-12" "2009-07-19" "2009-07-26" "2009-08-02" "2009-08-09" "2009-08-16" "2009-08-23" "2009-08-30"
 [73] "2009-09-06" "2009-09-13" "2009-09-20" "2009-09-27" "2009-10-04" "2009-10-11" "2009-10-18" "2009-10-25"
 [81] "2009-11-01" "2009-11-08" "2009-11-15" "2009-11-22" "2009-11-29" "2009-12-06" "2009-12-13" "2009-12-20"
 [89] "2009-12-27" "2010-01-03" "2010-01-10" "2010-01-17" "2010-01-24" "2010-01-31" "2010-02-07" "2010-02-14"
 [97] "2010-02-21" "2010-02-28" "2010-03-07" "2010-03-14" "2010-03-21" "2010-03-28" "2010-04-04" "2010-04-11"
[105] "2010-04-18" "2010-04-25" "2010-05-02" "2010-05-09" "2010-05-16" "2010-05-23" "2010-05-30" "2010-06-06"
[113] "2010-06-13" "2010-06-20" "2010-06-27" "2010-07-04" "2010-07-11" "2010-07-18" "2010-07-25" "2010-08-01"
[121] "2010-08-08" "2010-08-15" "2010-08-22" "2010-08-29" "2010-09-05" "2010-09-12" "2010-09-19" "2010-09-26"
[129] "2010-10-03" "2010-10-10" "2010-10-17" "2010-10-24" "2010-10-31" "2010-11-07" "2010-11-14" "2010-11-21"
[137] "2010-11-28" "2010-12-05" "2010-12-12" "2010-12-19" "2010-12-26" "2011-01-02" "2011-01-09" "2011-01-16"
[145] "2011-01-23" "2011-01-30" "2011-02-06" "2011-02-13" "2011-02-20" "2011-02-27" "2011-03-06" "2011-03-13"
[153] "2011-03-20" "2011-03-27" "2011-04-03" "2011-04-10" "2011-04-17" "2011-04-24" "2011-05-01" "2011-05-08"
[161] "2011-05-15" "2011-05-22" "2011-05-29" "2011-06-05" "2011-06-12" "2011-06-19" "2011-06-26" "2011-07-03"
[169] "2011-07-10" "2011-07-17" "2011-07-24" "2011-07-31" "2011-08-07" "2011-08-14" "2011-08-21" "2011-08-28"
[177] "2011-09-04" "2011-09-11" "2011-09-18" "2011-09-25" "2011-10-02" "2011-10-09" "2011-10-16" "2011-10-23"
[185] "2011-10-30" "2011-11-06" "2011-11-13" "2011-11-20" "2011-11-27" "2011-12-04" "2011-12-11" "2011-12-18"
[193] "2011-12-25" "2012-01-01" "2012-01-08" "2012-01-15" "2012-01-22" "2012-01-29" "2012-02-05" "2012-02-12"
[201] "2012-02-19" "2012-02-26" "2012-03-04" "2012-03-11" "2012-03-18" "2012-03-25" "2012-04-01" "2012-04-08"
[209] "2012-04-15" "2012-04-22" "2012-04-29" "2012-05-06" "2012-05-13" "2012-05-20" "2012-05-27" "2012-06-03"
[217] "2012-06-10" "2012-06-17" "2012-06-24" "2012-07-01" "2012-07-08" "2012-07-15" "2012-07-22" "2012-07-29"
[225] "2012-08-05" "2012-08-12" "2012-08-19" "2012-08-26" "2012-09-02" "2012-09-09" "2012-09-16" "2012-09-23"
[233] "2012-09-30" "2012-10-07" "2012-10-14" "2012-10-21" "2012-10-28" "2012-11-04" "2012-11-11" "2012-11-18"
[241] "2012-11-25" "2012-12-02" "2012-12-09" "2012-12-16" "2012-12-23" "2012-12-30" "2013-01-06" "2013-01-13"
[249] "2013-01-20" "2013-01-27" "2013-02-03" "2013-02-10" "2013-02-17" "2013-02-24" "2013-03-03" "2013-03-10"
[257] "2013-03-17" "2013-03-24" "2013-03-31" "2013-04-07" "2013-04-14" "2013-04-21" "2013-04-28" "2013-05-05"
[265] "2013-05-12" "2013-05-19" "2013-05-26" "2013-06-02" "2013-06-09" "2013-06-16" "2013-06-23" "2013-06-30"
[273] "2013-07-07" "2013-07-14" "2013-07-21" "2013-07-28" "2013-08-04" "2013-08-11" "2013-08-18" "2013-08-25"
[281] "2013-09-01" "2013-09-08" "2013-09-15" "2013-09-22" "2013-09-29" "2013-10-06" "2013-10-13" "2013-10-20"
[289] "2013-10-27" "2013-11-03" "2013-11-10" "2013-11-17" "2013-11-24" "2013-12-01" "2013-12-08" "2013-12-15"
[297] "2013-12-22" "2013-12-29" "2014-01-05" "2014-01-12" "2014-01-19" "2014-01-26" "2014-02-02" "2014-02-09"
[305] "2014-02-16" "2014-02-23" "2014-03-02" "2014-03-09" "2014-03-16" "2014-03-23" "2014-03-30" "2014-04-06"
[313] "2014-04-13" "2014-04-20" "2014-04-27" "2014-05-04" "2014-05-11" "2014-05-18" "2014-05-25" "2014-06-01"
[321] "2014-06-08" "2014-06-15" "2014-06-22" "2014-06-29" "2014-07-06" "2014-07-13" "2014-07-20" "2014-07-27"
[329] "2014-08-03" "2014-08-10" "2014-08-17" "2014-08-24" "2014-08-31" "2014-09-07" "2014-09-14" "2014-09-21"
[337] "2014-09-28" "2014-10-05" "2014-10-12" "2014-10-19" "2014-10-26" "2014-11-02" "2014-11-09" "2014-11-16"
[345] "2014-11-23" "2014-11-30" "2014-12-07" "2014-12-14" "2014-12-21" "2014-12-28" "2015-01-04" "2015-01-11"
[353] "2015-01-18" "2015-01-25" "2015-02-01" "2015-02-08" "2015-02-15" "2015-02-22" "2015-03-01" "2015-03-08"
[361] "2015-03-15" "2015-03-22" "2015-03-29" "2015-04-05" "2015-04-12" "2015-04-19" "2015-04-26" "2015-05-03"
[369] "2015-05-10" "2015-05-17" "2015-05-24" "2015-05-31" "2015-06-07" "2015-06-14" "2015-06-21" "2015-06-28"
[377] "2015-07-05" "2015-07-12" "2015-07-19" "2015-07-26" "2015-08-02" "2015-08-09" "2015-08-16" "2015-08-23"
[385] "2015-08-30" "2015-09-06" "2015-09-13" "2015-09-20" "2015-09-27" "2015-10-04" "2015-10-11" "2015-10-18"
[393] "2015-10-25" "2015-11-01" "2015-11-08" "2015-11-15" "2015-11-22" "2015-11-29" "2015-12-06" "2015-12-13"
[401] "2015-12-20" "2015-12-27" "2016-01-03" "2016-01-10" "2016-01-17" "2016-01-24" "2016-01-31" "2016-02-07"
[409] "2016-02-14" "2016-02-21" "2016-02-28" "2016-03-06" "2016-03-13" "2016-03-20" "2016-03-27" "2016-04-03"
[417] "2016-04-10" "2016-04-17" "2016-04-24" "2016-05-01" "2016-05-08" "2016-05-15" "2016-05-22" "2016-05-29"
[425] "2016-06-05" "2016-06-12" "2016-06-19" "2016-06-26" "2016-07-03" "2016-07-10" "2016-07-17" "2016-07-24"
[433] "2016-07-31" "2016-08-07" "2016-08-14" "2016-08-21" "2016-08-28" "2016-09-04" "2016-09-11" "2016-09-18"
[441] "2016-09-25" "2016-10-02" "2016-10-09" "2016-10-16" "2016-10-23" "2016-10-30" "2016-11-06" "2016-11-13"
[449] "2016-11-20" "2016-11-27" "2016-12-04" "2016-12-11" "2016-12-18" "2016-12-25" "2017-01-01" "2017-01-08"
[457] "2017-01-15" "2017-01-22" "2017-01-29" "2017-02-05" "2017-02-12" "2017-02-19" "2017-02-26" "2017-03-05"
[465] "2017-03-12" "2017-03-19" "2017-03-26" "2017-04-02" "2017-04-09" "2017-04-16" "2017-04-23" "2017-04-30"
[473] "2017-05-07" "2017-05-14" "2017-05-21" "2017-05-28" "2017-06-04" "2017-06-11" "2017-06-18" "2017-06-25"
[481] "2017-07-02" "2017-07-09" "2017-07-16" "2017-07-23" "2017-07-30" "2017-08-06" "2017-08-13" "2017-08-20"
[489] "2017-08-27" "2017-09-03" "2017-09-10" "2017-09-17" "2017-09-24" "2017-10-01" "2017-10-08" "2017-10-15"
[497] "2017-10-22" "2017-10-29" "2017-11-05" "2017-11-12" "2017-11-19" "2017-11-26" "2017-12-03" "2017-12-10"
[505] "2017-12-17" "2017-12-24" "2017-12-31" "2018-01-07" "2018-01-14" "2018-01-21" "2018-01-28" "2018-02-04"
[513] "2018-02-11" "2018-02-18" "2018-02-25" "2018-03-01"

$CPIAUCSL
  [1] 215.2080 215.2080 215.2080 215.7717 216.3355 216.8992 217.4630 217.7736 218.0842 218.3948 218.7054
 [12] 219.0160 218.9345 218.8530 218.7715 218.6900 218.7274 218.7648 218.8022 218.8396 218.8770 218.4065
 [23] 217.9360 217.4655 216.9950 216.0345 215.0740 214.1135 213.1530 212.8020 212.4510 212.1000 211.7490
 [34] 211.3980 211.5317 211.6655 211.7993 211.9330 212.1260 212.3190 212.5120 212.7050 212.6525 212.6000
 [45] 212.5475 212.4950 212.5378 212.5806 212.6234 212.6662 212.7090 212.7873 212.8655 212.9437 213.0220
 [56] 213.3756 213.7292 214.0828 214.4364 214.7900 214.7740 214.7580 214.7420 214.7260 214.9058 215.0855
 [67] 215.2652 215.4450 215.5282 215.6114 215.6946 215.7778 215.8610 216.0230 216.1850 216.3470 216.5090
 [78] 216.6902 216.8715 217.0528 217.2340 217.2566 217.2792 217.3018 217.3244 217.3470 217.3822 217.4175
 [89] 217.4528 217.4880 217.4466 217.4052 217.3638 217.3224 217.2810 217.2990 217.3170 217.3350 217.3530

解决方案

for (i in parsedList)
{

  past = lag(zoo(c(new[[i]])), c(-1,-2, -3, -4, -5), na.pad =TRUE)

  print(temp)

} 

1 个答案:

答案 0 :(得分:1)

Error: unexpected '}' in "}"错误来自语法错误。在c()

前面有一个多余的左括号
a=1
for (i in parsedList)
{
  #doesn't work in a loop
  past = lag(c(new[a]), c(-1,-2, -3, -4, -5), na.pad =TRUE)
  a=a+1
} 

我希望你之前定义'new',否则你会收到错误: new [a]中的错误:'closure'类型的对象不是子集 (因为它是用于面向对象编程的函数,用于创建新对象)。