r - 创建时间序列观察、时间戳和填充值
问题描述
我有一个横截面数据如下:
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 个月)代表各月份每笔贷款的到期时间。非常感谢您的帮助。谢谢
解决方案
这是一种使用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
推荐阅读
- javascript - 使用 BeautifulSoup (python) 在表中找不到值
- java - 仅允许在源级别 1.8 或更高版本中引用接口静态方法
- c++ - 为什么我的嵌套循环会产生相同的输出?
- ios - 如何在单个视图中重复播放两个不同的本地视频
- python - 两个 Pandas 数据框之间的交叉引用
- python-3.x - 字符串中的索引 - 绘图图
- handlebars.js - 拖放 div 列
- java - 在会话中存储和检索数组列表的问题
- angular - 如何在打字稿中提交表单(发布方法)后立即从服务器获取生成的ID
- wordpress - Cloudfront 似乎并没有提高网站的性能