r - 如何在R中按月重新采样?
问题描述
我试图找到一种在 r 中按月重新采样时间序列数据的方法。
这可以使用 Python 中的数据帧重采样来完成。有没有办法我们可以在 R 中做同样的事情?
初始数据框
Date Qty
05/25/2018 10
08/20/2018 15
10/15/2018 25
应改为
Date Qty
03/01/2018 0
04/01/2018 0
05/01/2018 10
06/01/2018 0
07/01/2018 0
08/01/2018 15
09/01/2018 0
10/01/2018 25
11/01/2018 0
12/01/2018 0
解决方案
data.table
和lubridate
接近
library( data.table )
library( lubridate )
dt <- fread("Date Qty
05/25/2018 10
08/20/2018 15
10/15/2018 25", header = TRUE)
#create data.table with first day of each month
dt.months <- data.table( Date = seq( as.Date("2018-01-01"), length=12, by="1 month"))
#set Date as actual date, and floor to the first day of the month
dt[, Date := floor_date( as.Date( Date, format = "%m/%d/%Y"), "month" )]
#sum qty by month (noft needed in this example)
#left join
result <- dt[dt.months, on = "Date", nomatch = NA ]
#replace NA with 0
result[is.na(result)] <- 0
result
Date Qty
# 1: 2018-01-01 0
# 2: 2018-02-01 0
# 3: 2018-03-01 0
# 4: 2018-04-01 0
# 5: 2018-05-01 10
# 6: 2018-06-01 0
# 7: 2018-07-01 0
# 8: 2018-08-01 15
# 9: 2018-09-01 0
# 10: 2018-10-01 25
# 11: 2018-11-01 0
# 12: 2018-12-01 0