R 将季度数据分解为每日数据,以保留数值?

R 将季度数据分解为每日数据,以保留数值?,r,dplyr,lubridate,R,Dplyr,Lubridate,如何轻松地将季度数据分解为每日数据?在本例中,我使用的是10年的美国GDP数据,这些数据有季度观测值,我想将数据框架扩展到每日水平,将每天的GDP值延续到下一次观测值 Reprex表: structure(list(thedate = structure(c(14426, 14518, 14610, 14700, 14791, 14883, 14975, 15065, 15156, 15248, 15340, 15431, 15522, 15614, 15706, 15796, 15887,

如何轻松地将季度数据分解为每日数据?在本例中,我使用的是10年的美国GDP数据,这些数据有季度观测值,我想将数据框架扩展到每日水平,将每天的GDP值延续到下一次观测值

Reprex表:

structure(list(thedate = structure(c(14426, 14518, 14610, 14700, 
14791, 14883, 14975, 15065, 15156, 15248, 15340, 15431, 15522, 
15614, 15706, 15796, 15887, 15979, 16071, 16161, 16252, 16344, 
16436, 16526, 16617, 16709, 16801, 16892, 16983, 17075, 17167, 
17257, 17348, 17440, 17532, 17622, 17713, 17805, 17897, 17987
), class = "Date"), gdp = c(1.5, 4.5, 1.5, 3.7, 3, 2, -1, 2.9, 
-0.1, 4.7, 3.2, 1.7, 0.5, 0.5, 3.6, 0.5, 3.2, 3.2, -1.1, 5.5, 
5, 2.3, 3.2, 3, 1.3, 0.1, 2, 1.9, 2.2, 2, 2.3, 2.2, 3.2, 3.5, 
2.5, 3.5, 2.9, 1.1, 3.1, 2.1)), class = "data.frame", row.names = c(NA, 
-40L))
我们在上面看到:

2009-07-01 | 1.5
2009-10-01 | 4.5
预期输出如下所示:

2009-07-01 | 1.5
2009-07-02 | 1.5
2009-07-03 | 1.5
etc.
2009-10-01 | 4.5
2009-10-02 | 4.5
2009-10-03 | 4.5

以下是一个基本解决方案:

last_quarter_end_date <- seq.Date(df$thedate[nrow(df)], by = 'quarter', length.out = 2)[-1]-1
seqs <- diff(c(df$thedate, last_quarter_end_date))

data.frame(thedate = rep(df$thedate, seqs) + sequence(seqs)-1
           , gdp = rep(df$gdp, seqs))
完整代码供参考:

DF <- structure(list(thedate = structure(c(14426, 14518, 14610, 14700, 
                                           14791, 14883, 14975, 15065, 15156, 15248, 15340, 15431, 15522, 
                                           15614, 15706, 15796, 15887, 15979, 16071, 16161, 16252, 16344, 
                                           16436, 16526, 16617, 16709, 16801, 16892, 16983, 17075, 17167, 
                                           17257, 17348, 17440, 17532, 17622, 17713, 17805, 17897, 17987
), class = "Date"), gdp = c(1.5, 4.5, 1.5, 3.7, 3, 2, -1, 2.9, 
                            -0.1, 4.7, 3.2, 1.7, 0.5, 0.5, 3.6, 0.5, 3.2, 3.2, -1.1, 5.5, 
                            5, 2.3, 3.2, 3, 1.3, 0.1, 2, 1.9, 2.2, 2, 2.3, 2.2, 3.2, 3.5, 
                            2.5, 3.5, 2.9, 1.1, 3.1, 2.1)), class = "data.frame", row.names = c(NA, 
                                                                                                -40L))

library(microbenchmark)
library(tidyr)

microbenchmark(cole_base = {
  last_quarter_end_date <- seq.Date(DF$thedate[nrow(DF)], by = 'quarter', length.out = 2)[-1]-1
  seqs <- diff(c(DF$thedate, last_quarter_end_date))

  data.frame(thedate = rep(DF$thedate, seqs) + sequence(seqs)-1
             , gdp = rep(DF$gdp, seqs))
}
, d_b_base = {
  do.call(rbind, lapply(2:NROW(DF), function(i){
    data.frame(date = head(seq.Date(DF$thedate[i-1], DF$thedate[i], "days"), -1),
               gdp = DF$gdp[i - 1])
     }))
}
, Ben_tidyr = {
  DF %>%
    complete(thedate = seq.Date(min(thedate), max(thedate), by="day")) %>%
    fill(gdp)
}
)

DF这是一个tidyr和zoo软件包的答案,在插入带有NA的日期序列后使用“最后一次观察结转”:

library(tidyverse)
library(zoo)

data %>%
  complete(thedate = seq.Date(min(thedate), max(thedate), by="day")) %>%
  do(na.locf(.))
编辑:感谢Shree提醒我们,tidyr::fill将不再需要动物园:

library(tidyverse)

data %>%
  complete(thedate = seq.Date(min(thedate), max(thedate), by="day")) %>%
  fill(gdp)
tidyr::fill(gdp)
也会这样做,以防您想坚持使用
tidyverse
library(tidyverse)
library(zoo)

data %>%
  complete(thedate = seq.Date(min(thedate), max(thedate), by="day")) %>%
  do(na.locf(.))
library(tidyverse)

data %>%
  complete(thedate = seq.Date(min(thedate), max(thedate), by="day")) %>%
  fill(gdp)