R LOCF直到xts对象中的月末

时间:2017-10-21 12:56:56

标签: r xts

我正在努力找到一种很好的方法,可以在我的xts对象中将月中发生的最后一个值传送到月末。

2010-02-26     4029.027
2010-02-27     4029.027
2010-02-28     4029.027
2010-03-04     4029.027
2010-03-05     4029.027
2010-03-20     4029.027
2010-03-26     4029.027
2010-03-27     4029.027
2010-03-28     4029.027
2010-03-31     4029.027
2010-04-02     4029.027
2010-04-03     5956.582
2010-04-04           NA
2010-04-11           NA
2010-04-24           NA
2010-04-25           NA
2010-04-28           NA
2010-04-30           NA
2010-05-01           NA

从我上面的数据中可以看出,我有" NA""在2010-04之后,理想情况下我希望将5956.582推进到月底,所以我的数据看起来像:

2010-02-26     4029.027
2010-02-27     4029.027
2010-02-28     4029.027
2010-03-04     4029.027
2010-03-05     4029.027
2010-03-20     4029.027
2010-03-26     4029.027
2010-03-27     4029.027
2010-03-28     4029.027
2010-03-31     4029.027
2010-04-02     4029.027
2010-04-03     5956.582
2010-04-04     5956.582
2010-04-11     5956.582
2010-04-24     5956.582
2010-04-25     5956.582
2010-04-28     5956.582
2010-04-30     5956.582
2010-05-01           NA

在我开始编写自己的函数之前,我想知道是否有人知道另一种方式?

由于

ST

2 个答案:

答案 0 :(得分:2)

ave与动态包中的as.yearmonna.locf0一起使用(xts加载)。除了你已经使用的xts / zoo之外,这不会使用任何其他软件包。

library(xts)
ave(x, as.yearmon(time(x)), FUN = na.locf0)

,并提供:

               [,1]
2010-02-26 4029.027
2010-02-27 4029.027
2010-02-28 4029.027
2010-03-04 4029.027
2010-03-05 4029.027
2010-03-20 4029.027
2010-03-26 4029.027
2010-03-27 4029.027
2010-03-28 4029.027
2010-03-31 4029.027
2010-04-02 4029.027
2010-04-03 5956.582
2010-04-04 5956.582
2010-04-11 5956.582
2010-04-24 5956.582
2010-04-25 5956.582
2010-04-28 5956.582
2010-04-30 5956.582
2010-05-01       NA

注意:

可重现形式的输入x是:

Lines <- "
2010-02-26     4029.027
2010-02-27     4029.027
2010-02-28     4029.027
2010-03-04     4029.027
2010-03-05     4029.027
2010-03-20     4029.027
2010-03-26     4029.027
2010-03-27     4029.027
2010-03-28     4029.027
2010-03-31     4029.027
2010-04-02     4029.027
2010-04-03     5956.582
2010-04-04           NA
2010-04-11           NA
2010-04-24           NA
2010-04-25           NA
2010-04-28           NA
2010-04-30           NA
2010-05-01           NA"

library(xts)

z <- read.zoo(text = Lines)
x <- as.xts(z)

答案 1 :(得分:0)

尝试此操作,使用zoo::na.locf填写NA

您的数据

df <- read.table(text="2010-02-26     4029.027
2010-02-27     4029.027
2010-02-28     4029.027
2010-03-04     4029.027
2010-03-05     4029.027
2010-03-20     4029.027
2010-03-26     4029.027
2010-03-27     4029.027
2010-03-28     4029.027
2010-03-31     4029.027
2010-04-02     4029.027
2010-04-03     5956.582
2010-04-04           NA
2010-04-11           NA
2010-04-24           NA
2010-04-25           NA
2010-04-28           NA
2010-04-30           NA
2010-05-01           NA", header=FALSE)

解决方案

library(dplyr)
library(zoo)
library(lubridate)

您的May数据是一个问题,因为它是该月的单NA次观察。这就是我必须使用if (!is.na(.x$V2))来调整操作mutate(V2 = na.locf(V2))

的原因
result <- df %>%
           mutate(V1 = ymd(V1)) %>%       # convert to Date just in case
           split(month(.$V1)) %>%         # split data by month
           map(., ~if (!is.na(.x$V2)) {.x %>% mutate(V2 = na.locf(V2))} else {.x})  # iterate through list by month
ans <- Reduce("rbind", result)

           # V1       V2
# 1  2010-02-26 4029.027
# 2  2010-02-27 4029.027
# 3  2010-02-28 4029.027
# 4  2010-03-04 4029.027
# 5  2010-03-05 4029.027
# 6  2010-03-20 4029.027
# 7  2010-03-26 4029.027
# 8  2010-03-27 4029.027
# 9  2010-03-28 4029.027
# 10 2010-03-31 4029.027
# 11 2010-04-02 4029.027
# 12 2010-04-03 5956.582
# 13 2010-04-04 5956.582
# 14 2010-04-11 5956.582
# 15 2010-04-24 5956.582
# 16 2010-04-25 5956.582
# 17 2010-04-28 5956.582
# 18 2010-04-30 5956.582
# 19 2010-05-01       NA