簡體   English   中英

如何在R中的XTS中拉出月份的最后n個交易日?

[英]How to pull last nth trading day of month in XTS in R?

這個問題與R中的XTS中的“每月第n個日”密切相關,在其中我得到了一個很好的答案:

library(xts)
data(sample_matrix)
x <- as.xts(sample_matrix)
do.call(rbind, lapply(split(x, "months"), function(x) x[10]))
#                Open     High      Low    Close
# 2007-01-11 49.88529 50.23910 49.88529 50.23910
# 2007-02-10 50.68923 50.72696 50.60707 50.69562
# 2007-03-10 49.79370 49.88984 49.70385 49.88698
# 2007-04-10 49.55704 49.78776 49.55704 49.76984
# 2007-05-10 48.83479 48.84549 48.38001 48.38001
# 2007-06-10 47.74899 47.74899 47.28685 47.28685

但是,我想拉每個月的最后 n個交易日。 例如,我有一個像這樣的數據框,但是時間跨度是幾年。

         date change   open   high    low  close    volume
1  1990-01-02  1.780 353.40 359.69 351.98 359.69 162070000
2  1990-01-03 -0.259 359.69 360.59 357.89 358.76 192330000
3  1990-01-04 -0.861 358.76 358.76 352.89 355.67 177000000
4  1990-01-05 -0.976 355.67 355.67 351.35 352.20 158530000
5  1990-01-08  0.451 352.20 354.24 350.54 353.79 140110000
6  1990-01-09 -1.179 353.83 354.17 349.61 349.62 155210000
7  1990-01-10 -0.661 349.62 349.62 344.32 347.31 175990000
8  1990-01-11  0.351 347.31 350.14 347.31 348.53 154390000
9  1990-01-12 -2.468 348.53 348.53 339.49 339.93 183880000
10 1990-01-15 -0.862 339.93 339.94 336.57 337.00 140590000
11 1990-01-16  1.113 337.00 340.75 333.37 340.75 186070000
12 1990-01-17 -0.983 340.77 342.01 336.26 337.40 170470000
13 1990-01-18  0.234 337.40 338.38 333.98 338.19 178590000
14 1990-01-19  0.284 338.19 340.48 338.19 339.15 185590000
15 1990-01-22 -2.586 339.14 339.96 330.28 330.38 148380000
16 1990-01-23  0.372 330.38 332.76 328.67 331.61 179300000
17 1990-01-24 -0.407 331.61 331.71 324.17 330.26 207830000

我想有提取每個月的最后第n個交易日的功能,並形成新的數據幀。 例如,如果我想提取每月的最后第二個交易日。 輸出應如下表所示。

      date change   open   high    low  close    volume
1990-01-30 -0.683 325.20 325.73 319.83 322.98 186030000
1990-02-27  0.484 328.68 331.94 328.47 330.26 152590000
1990-03-29 -0.354 342.00 342.07 339.77 340.79 132190000
1990-04-27 -1.144 332.92 333.57 328.71 329.11 130630000

請注意, 我要提取每個月的最后一個數據點,而不是最后一個日歷日期。

您可以使用tail

n <- 2
do.call(rbind, lapply(split(x, "months"), function(x) tail(x, n)))

#                Open     High      Low    Close
# 2007-01-30 49.85477 50.02180 49.77242 50.02180
# 2007-01-31 50.07049 50.22578 50.07049 50.22578
# 2007-02-27 50.74333 50.78909 50.61874 50.69206
# 2007-02-28 50.69435 50.77091 50.59881 50.77091
# 2007-03-30 48.74562 49.00218 48.74562 48.93546
# 2007-03-31 48.95616 49.09728 48.95616 48.97490
# 2007-04-29 49.30289 49.30289 49.05676 49.13529
# 2007-04-30 49.13825 49.33974 49.11500 49.33974
# 2007-05-30 47.78866 47.93267 47.78866 47.83291
# 2007-05-31 47.82845 47.84044 47.73780 47.73780
# 2007-06-29 47.63629 47.77563 47.61733 47.66471
# 2007-06-30 47.67468 47.94127 47.67468 47.76719

數據

library(xts)
data(sample_matrix)
x <- as.xts(sample_matrix)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM