这是一个基本解决方案:
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))
基本上,日期之间的区别在于需要重复 GDP 列的次数。此外,我可以对每个差异使用 seq_len()
,并将其添加回原始日期。
性能
虽然我要指出,在大局观中,0.6 毫秒与 15 毫秒的差异并不太大,但这种方法还是很高效的。
Unit: microseconds
expr min lq mean median uq max neval
cole_base 528.1 554.15 690.379 644.9 663.75 3225.7 100
d_b_base 15735.0 15994.40 17395.754 16243.9 18108.30 38761.8 100
Ben_tidyr 2808.7 2936.40 3356.324 3076.6 3149.65 8065.1 100
完整的代码供参考:
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)
}
)
tidyr::fill(gdp)
也可以实现,如果您想坚持使用tidyverse
。 - Shree