R LOCF 直到 xts 对象中的月底
R LOCF until end of Month in xts object
我正在努力寻找一种很好的方法来将月中发生的最后一个值向前传递到我的 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
正如您从我上面的数据中看到的那样,我在 2010-04 之后有 "NA's",理想情况下我想将 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
试试这个,它使用 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
将 ave
与 zoo 包(xts 加载)中的 as.yearmon
和 na.locf0
一起使用。这不会使用除 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)
我正在努力寻找一种很好的方法来将月中发生的最后一个值向前传递到我的 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
正如您从我上面的数据中看到的那样,我在 2010-04 之后有 "NA's",理想情况下我想将 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
试试这个,它使用 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
将 ave
与 zoo 包(xts 加载)中的 as.yearmon
和 na.locf0
一起使用。这不会使用除 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)