通过每天重复每月的值,是否有R函数可以将月度数据转换为每日数据?

时间:2020-06-08 16:06:15

标签: r

假设我有每月的利率。我想将此信息表示为“每日”利率,并让每月利率每天重复一次,直到新月更新为止。目的是用于每日报告,利率是指标之一。

假设我的日期列是年月日...

df.int = data.frame(c('2020-01-01','2020-01-02','2020-01-03','2020-01-04'),
                    c(0.02321,0.02198,0.01985,0.02011))

colnames(df.int) = c('Date','Interest Rate')

关于如何执行此操作的任何建议?

2 个答案:

答案 0 :(得分:0)

我们可以创建所需长度的每日日期范围,然后根据这些日期的月份加入该日期范围:

\n

数据

library(lubridate)
library(tidyverse)

#convert Date to Date and create month variable
df.int <- df.int %>% 
  mutate(Date = ydm(Date),
         month = month(Date))

#create daily sequence with min-max date range based on min/max date of df.int$Date
daily_df <- tibble(date = seq(min(df.int$Date), max(df.int$Date), "days")) %>% 
  mutate(month = month(date))

#join based on month
daily_df %>% 
  left_join(df.int, "month") %>% 
  select(-date)

# A tibble: 92 x 3
   month Date       `Interest Rate`
   <dbl> <date>               <dbl>
 1     1 2020-01-01          0.0232
 2     1 2020-01-01          0.0232
 3     1 2020-01-01          0.0232
 4     1 2020-01-01          0.0232
 5     1 2020-01-01          0.0232
 6     1 2020-01-01          0.0232
 7     1 2020-01-01          0.0232
 8     1 2020-01-01          0.0232
 9     1 2020-01-01          0.0232
10     1 2020-01-01          0.0232
# ... with 82 more rows

答案 1 :(得分:0)

经过更多研究后,我找到了该线程,回答了我的问题!

Converting Monthly Data to Daily in R

相关问题