使用财务包的摊销时间表

Amortisation schedule using package financial

如何将包 financialtvm() 生成的摊销时间表读入数据框?

示例代码

y=tvm(pv=10000,i=10,n=10,pmt=NA)
summary.tvm(y)

结果

> summary.tvm(y)

Amortization Table

       Bal    Int   Prin    PMT
1     9037  83.33   -963  -1046
2     8066  75.31   -971  -1046
3     7087  67.22   -979  -1046
4     6099  59.06   -987  -1046
5     5104  50.83   -996  -1046
6     4100  42.53  -1004  -1046
7     3088  34.17  -1012  -1046
8     2067  25.73  -1021  -1046
9     1038  17.22  -1029  -1046
10       0   8.65  -1038  -1046
Total      464.04 -10000 -10464

很明显,我需要将这个摘要分配给数据框,当我检查 summary.tvm() 的函数源代码时,它显示了这个。

> summary.tvm
function (object, row = 1, ...) 
{
    cat("\nAmortization Table\n\n")
    x = object
    row = x[row, ]
    n = row[2]
    a = row[7]
    i = row[1]/(100 * row[8])
    pv = row[3]
    fv = row[4]
    pmt = row[5]
    days = row[6]
    pyr = row[8]
    bal = pv + a * pmt
    res = c()
    for (k in 1:(n - a)) {
        if (k == 1) {
            int = bal * i * (days/(360/pyr))
            prin = pmt + int
            bal = bal + prin
            prin = prin + a * pmt
            res = rbind(res, c(bal, int, prin, pmt * (1 + a)))
        }
        else {
            int = bal * i
            prin = pmt + int
            bal = bal + prin
            res = rbind(res, c(bal, int, prin, pmt))
        }
    }
    res = rbind(res, c(NA, sum(res[, 2]), sum(res[, 3]), sum(res[, 
        4])))
    colnames(res) = c("Bal", "Int", "Prin", "PMT")
    rownames(res) = c(1:(n - a), "Total")
    print(round(res, 2), na.print = "")
    invisible(res)
}
<bytecode: 0x000000001cd42768>
<environment: namespace:financial>

我想我需要以某种方式提取 res 并将其分配给数据框。如何做到这一点?我检查了 summary.tvm 输出的 class,它显示了 matrix.

只需将结果分配给一个对象。如果对象未分配给变量,则 ˙invisible` 部分只会抑制对象的打印。

out <- summary.tvm(y)

结果应该是一个矩阵,是什么让您认为它应该是其他东西?如果你想要 data.frame,试试 as.data.frame(out)

在一个简短的例子中:

> smr <- function(x) {
+   xy <- matrix(1:x, nrow = 1)
+   print(xy)
+   invisible(xy)
+ }
> out <- smr(10)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    2    3    4    5    6    7    8    9    10
> as.data.frame(out)
  V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1  1  2  3  4  5  6  7  8  9  10