简体   繁体   English

如何在 xts 上填写没有数据的日期

[英]How do I fill in dates without data on xts

Say I have data xts object (data) for 30 June, 31 July, 29 August, and 30 September.假设我有 6 月 30 日、7 月 31 日、8 月 29 日和 9 月 30 日的数据 xts object(数据)。

I have a date object (dates) with 30 June, 31 July, 31 August, and 30 September.我有一个日期 object(日期)与 6 月 30 日、7 月 31 日、8 月 31 日和 9 月 30 日。 When I try to do new_data <- data[dates], it just skips the August data.当我尝试执行 new_data <- data[dates] 时,它只是跳过了 8 月的数据。

I would like to use data as EOM data for these four months.我想将这四个月的数据用作 EOM 数据。 What can I do to not skip the August date, and use the 29 August data for 31 August?我该怎么做才能不跳过 8 月的日期,并使用 8 月 31 日的 8 月 29 日数据?

Here I'm assuming "EOM" (end of month) data means the last trading day of the month is included in your time series, which doesnt include weekends (based on your description).在这里,我假设“EOM”(月末)数据意味着该月的最后一个交易日包含在您的时间序列中,不包括周末(根据您的描述)。

You can split an xts object by month, and take the last value, assuming you're working with daily data (1 bar is 1 day)您可以按月拆分xts object 并取最后一个值,假设您使用的是每日数据(1 个柱形图为 1 天)

x1 <- getSymbols("AAPL", auto.assign = FALSE)
yy2 <- do.call(rbind, lapply(split(x1, by = "months"), tail, n = 1))

tail(yy2, 10)

#AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
#2021-03-31    121.65    123.52   121.15     122.15   118323800      121.5830
#2021-04-30    131.78    133.56   131.07     131.46   109839500      130.8498
#2021-05-28    125.57    125.80   124.55     124.61    71311100      124.2423
#2021-06-30    136.17    137.41   135.87     136.96    63261400      136.5558
#2021-07-30    144.38    146.33   144.11     145.86    70382000      145.4295
#2021-08-31    152.66    152.80   151.29     151.83    86453100      151.6087
#2021-09-30    143.66    144.38   141.28     141.50    88934200      141.2938
#2021-10-29    147.22    149.94   146.41     149.80   124850400      149.5817
#2021-11-30    159.99    165.52   159.92     165.30   174048100      165.3000
#2021-12-31    178.09    179.23   177.26     177.57    64025500      177.5700

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM