【问题标题】:Create Time series observations,timestamps and filling up the values创建时间序列观察、时间戳和填充值
【发布时间】:2018-12-24 20:44:07
【问题描述】:

我有一个横截面数据如下:

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))

现在数据框看起来像这样

>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 个月内三笔贷款中每笔贷款的到期时间(以月为单位)。我怎样才能做到这一点?最终输出应如下所示:

>loan data
   transaction_code    loan_start_date    loan_maturity_date   feb13  march13 april13........
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

这里的新列(48 个月)代表各月份每笔贷款的到期时间。 非常感谢您的帮助。谢谢

【问题讨论】:

  • 您能否说明您希望如何计算“到期时间”?我还不明白它们与loan_maturity_date 的关系。例如,第三笔贷款的贷款到期日晚于第二笔贷款(2015 年与 2013 年相比),但右侧列中的数字更小。
  • 嗨@JonSpring,我只是假设了这些值。现在我已经修好了。第一行中 feb13 的值是 2013-02-28 和 2017-01-03 之间的月差。对于 March13,它是 2013-03-31 和 2017-01-03 之间的月份差异。等等……

标签: r time-series


【解决方案1】:

这是一种使用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

【讨论】:

    猜你喜欢
    • 2023-02-08
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多