创建时间序列观测值,时间戳并填充值

时间:2018-12-24 20:44:08

标签: r time-series

我的横截面数据如下:

with

现在数据框看起来像这样

transaction_code <- c('A_111','A_222','A_333')
loan_start_date <- c('2016-01-03','2011-01-08','2013-02-13')
loan_maturity_date <- c('2017-01-03','2013-01-08','2015-02-13')
loan_data <- data.frame(cbind(transaction_code,loan_start_date,loan_maturity_date))

现在,我想创建一个月度时间序列,以观察这三笔贷款在48个月内的到期时间(以月为单位)。我该如何实现?最终输出应如下所示:

>loan_data
    transaction_code    loan_start_date    loan_maturity_date
1            A_111      2016-01-03         2017-01-03
2            A_222      2011-01-08         2013-01-08
3            A_333      2013-02-13         2015-02-13

此处的新列(48个月)表示从该月份开始的每笔贷款的到期时间。 非常感谢您的帮助。谢谢

1 个答案:

答案 0 :(得分:0)

这是使用tidyverse软件包的一种方法。

# Define the months to use in the right-hand columns.
months <- seq.Date(from = as.Date("2013-02-01"), by = "month", length.out = 48)

library(tidyverse); library(lubridate)
loan_data2 <- loan_data %>%
  # Make a row for each combination of original data and the `months` list
  crossing(months) %>%
  # Format dates as MonYr and make into an ordered factor
  mutate(month_name = format(months, "%b%y") %>% fct_reorder(months)) %>%
  # Calculate months remaining -- this task is harder than it sounds! This
  #   approach isn't perfect, but it's hard to accomplish more simply, since
  #   months are different lengths. 
  mutate(months_remaining = 
           round(interval(months, loan_maturity_date) / ddays(1) / 30.5 - 1),
         months_remaining = if_else(months_remaining < 0, 
                                    NA_real_, months_remaining)) %>%
  # Drop the Date format of months now that calcs done
  select(-months) %>%
  # Spread into wide format
  spread(month_name, months_remaining)

输出

loan_data2[,1:6]
#   transaction_code loan_start_date loan_maturity_date Feb13 Mar13 Apr13
# 1            A_111      2016-01-03         2017-01-03    46    45    44
# 2            A_222      2011-01-08         2013-01-08    NA    NA    NA
# 3            A_333      2013-02-13         2015-02-13    23    22    21