我正在处理的是1990年至2018年数千家上市公司的资产负债表时间序列。在某些年份,我只有年度资产负债表,而在其他年份,我有5份资产负债表,包括1份年度资产负债表和4份季度资产负债表。我正在尝试使用所有可用的信息。资产负债表的日期总是在xxxx-01-01/xxxx-03-31/xxxx-06-30/xxxx-09-30/xxxx-12-31.上我选择代码号、日期、长期责任和短期责任.我想首先计算长期和短期负债的总和作为一个新的列,并做一个线性脊柱内插的新列的代码编号为每个月。日期是月年的形式。
code date type cl ll
1 1990-12-31 A 56280000 0
1 1991-12-31 A 77230000 0
1 1992-12-31 A 195893200 0
1 1993-01-01 A 0 0
1 1994-06-30 A 0 0
1 1994-12-31 A 0 0
1 1996-12-31 A 0 0
2 1991-12-31 A 374334527 3500000
2 1992-12-31 A 688472115 19820785
2 1993-12-31 A 1135584690 70268722
2 1994-12-31 A 1442120726 85175588
2 1995-06-30 A 1571620470 0 当时间间隔为常数时,我知道如何使用样条函数和na.approx。但我不知道如何处理非恒定的时间间隔。谢谢!
发布于 2019-01-17 02:57:22
我刚刚得到了想要的结果。这个想法来自一个类似的问题-- https://stackoverflow.com/a/31383995/10714457。值得注意的是,月份栏是以字符形式出现的。我需要as.numeric(month)先把它们转换成数字。
DF$month <- format(as.Date(DF$date), "%m")
DF$year <- format(as.Date(DF$date), "%Y")
res <- setDT(DF)[, .SD[match(1:12, as.numeric(month))], by = .(year, code)]
cols <- c("ll", "cl", "ncl")
Interpolation <- res[, (cols) :=
lapply(.SD, na.approx, na.rm = FALSE), .SDcols = cols]https://stackoverflow.com/questions/54222816
复制相似问题