使用lapply
代替,sapply
它会起作用。
让我们先加载数据。
require(tseries)
require(zoo)
symbs = c('SPY', 'XLE', 'XLF', 'XLP', 'XLI', 'XLY', 'XLV', 'XLK', 'XLU')
importData <- lapply(symbs, function(sIdx)
get.hist.quote( instrument = sIdx, start="2000-01-01",
end="2013-07-15", quote="AdjClose", provider="yahoo",
origin="1970-01-01",
compression = "m", retclass="zoo"))
names(importData) <- symbs
str(importData, max.level = 1)
## List of 9
## $ SPY:‘zoo’ series from 2000-01-03 to 2013-07-01
## Data: num [1:163, 1] 109 107 118 114 112 ...
## ..- attr(*, "dimnames")=List of 2
## Index: Date[1:163], format: "2000-01-03" ...
## $ XLE:‘zoo’ series from 2000-01-03 to 2013-07-01
## Data: num [1:163, 1] 22.5 21.5 24.1 23.7 26.5 ...
## ..- attr(*, "dimnames")=List of 2
## Index: Date[1:163], format: "2000-01-03" ...
## $ XLF:‘zoo’ series from 2000-01-03 to 2013-07-01
## Data: num [1:163, 1] 17.8 15.9 18.7 18.9 19.3 ...
## ..- attr(*, "dimnames")=List of 2
## Index: Date[1:163], format: "2000-01-03" ...
## $ XLP:‘zoo’ series from 2000-01-03 to 2013-07-01
## Data: num [1:163, 1] 17.8 15.7 16.3 17.1 18.4 ...
## ..- attr(*, "dimnames")=List of 2
## Index: Date[1:163], format: "2000-01-03" ...
## $ XLI:‘zoo’ series from 2000-01-03 to 2013-07-01
## Data: num [1:163, 1] 21.9 20.7 23.6 23.9 23.8 ...
## ..- attr(*, "dimnames")=List of 2
## Index: Date[1:163], format: "2000-01-03" ...
## $ XLY:‘zoo’ series from 2000-01-03 to 2013-07-01
## Data: num [1:163, 1] 23.7 22.4 25.5 24.9 23.6 ...
## ..- attr(*, "dimnames")=List of 2
## Index: Date[1:163], format: "2000-01-03" ...
## $ XLV:‘zoo’ series from 2000-01-03 to 2013-07-01
## Data: num [1:163, 1] 25.3 23.7 25.8 25.5 24.8 ...
## ..- attr(*, "dimnames")=List of 2
## Index: Date[1:163], format: "2000-01-03" ...
## $ XLK:‘zoo’ series from 2000-01-03 to 2013-07-01
## Data: num [1:163, 1] 44.6 49.3 53.5 48.5 43.5 ...
## ..- attr(*, "dimnames")=List of 2
## Index: Date[1:163], format: "2000-01-03" ...
## $ XLU:‘zoo’ series from 2000-01-03 to 2013-07-01
## Data: num [1:163, 1] 17.6 15.4 17 18.1 18.1 ...
## ..- attr(*, "dimnames")=List of 2
## Index: Date[1:163], format: "2000-01-03" ...
现在我们可以测试它是否有效。
monthlyPctChgs <- lapply(importData, function(x) diff(x, lag =2) / lag(x, k=-2))
str(monthlyPctChgs)
## List of 9
## $ SPY:‘zoo’ series from 2000-03-01 to 2013-07-01
## Data: num [1:161, 1] 0.0803 0.05827 -0.05032 0.00369 0.00366 ...
## ..- attr(*, "dimnames")=List of 2
## .. ..$ : NULL
## .. ..$ : chr "AdjClose"
## Index: Date[1:161], format: "2000-03-01" ...
## $ XLE:‘zoo’ series from 2000-03-01 to 2013-07-01
## Data: num [1:161, 1] 0.073 0.1037 0.1012 0.056 -0.0912 ...
## ..- attr(*, "dimnames")=List of 2
## .. ..$ : NULL
## .. ..$ : chr "AdjClose"
## Index: Date[1:161], format: "2000-03-01" ...
## $ XLF:‘zoo’ series from 2000-03-01 to 2013-07-01
## Data: num [1:161, 1] 0.0529 0.1897 0.0321 -0.027 0.0347 ...
## ..- attr(*, "dimnames")=List of 2
## .. ..$ : NULL
## .. ..$ : chr "AdjClose"
## Index: Date[1:161], format: "2000-03-01" ...
## $ XLP:‘zoo’ series from 2000-03-01 to 2013-07-01
## Data: num [1:161, 1] -0.0864 0.0889 0.1284 0.1319 0.0174 ...
## ..- attr(*, "dimnames")=List of 2
## .. ..$ : NULL
## .. ..$ : chr "AdjClose"
## Index: Date[1:161], format: "2000-03-01" ...
## $ XLI:‘zoo’ series from 2000-03-01 to 2013-07-01
## Data: num [1:161, 1] 0.0738 0.1528 0.0106 -0.0431 -0.0248 ...
## ..- attr(*, "dimnames")=List of 2
## .. ..$ : NULL
## .. ..$ : chr "AdjClose"
## Index: Date[1:161], format: "2000-03-01" ...
## $ XLY:‘zoo’ series from 2000-03-01 to 2013-07-01
## Data: num [1:161, 1] 0.076 0.1158 -0.0738 -0.1054 -0.039 ...
## ..- attr(*, "dimnames")=List of 2
## .. ..$ : NULL
## .. ..$ : chr "AdjClose"
## Index: Date[1:161], format: "2000-03-01" ...
## $ XLV:‘zoo’ series from 2000-03-01 to 2013-07-01
## Data: num [1:161, 1] 0.0186 0.0769 -0.0384 -0.0247 -0.0129 ...
## ..- attr(*, "dimnames")=List of 2
## .. ..$ : NULL
## .. ..$ : chr "AdjClose"
## Index: Date[1:161], format: "2000-03-01" ...
## $ XLK:‘zoo’ series from 2000-03-01 to 2013-07-01
## Data: num [1:161, 1] 0.1976 -0.0158 -0.1862 -0.0146 0.0411 ...
## ..- attr(*, "dimnames")=List of 2
## .. ..$ : NULL
## .. ..$ : chr "AdjClose"
## Index: Date[1:161], format: "2000-03-01" ...
## $ XLU:‘zoo’ series from 2000-03-01 to 2013-07-01
## Data: num [1:161, 1] -0.0324 0.1729 0.0629 -0.0359 -0.0188 ...
## ..- attr(*, "dimnames")=List of 2
## .. ..$ : NULL
## .. ..$ : chr "AdjClose"
## Index: Date[1:161], format: "2000-03-01" ...
sapply
倾向于将结果简化为矩阵/向量,而lapply
总是返回一个列表,在这种情况下不可能返回一个矩阵。
更多信息在这里:R分组功能:sapply vs. lapply vs. apply。vs. tapply vs. by vs. aggregate
如果出于任何原因您想返回类似对象的矩阵,您可以像这样进行
monthly_pctchgs <- do.call(cbind, monthlyPctChgs)
colnames(monthly_pctchgs) <- symbs
str(monthly_pctchgs)
## ‘zoo’ series from 2000-03-01 to 2013-07-01
## Data: num [1:161, 1:9] 0.0803 0.05827 -0.05032 0.00369 0.00366 ...
## - attr(*, "dimnames")=List of 2
## ..$ : NULL
## ..$ : chr [1:9] "SPY" "XLE" "XLF" "XLP" ...
## Index: Date[1:161], format: "2000-03-01" "2000-04-03" "2000-05-01" ...
最后,这是我的会话信息
## sessionInfo()
## R version 3.0.1 Patched (2013-07-22 r63380)
## Platform: x86_64-unknown-linux-gnu (64-bit)
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
## attached base packages:
## [1] stats graphics grDevices utils datasets methods
## [7] base
## other attached packages:
## [1] zoo_1.7-10 tseries_0.10-32
## loaded via a namespace (and not attached):
## [1] compiler_3.0.1 grid_3.0.1 lattice_0.20-15
## [4] quadprog_1.5-5 tools_3.0.1