使用包金融的摊销计划

时间:2017-09-29 09:56:21

标签: r summary financial

如何阅读包tvm()中的financial生成的摊还计划到数据框中?

示例代码

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输出的类,它显示了matrix

1 个答案:

答案 0 :(得分:1)

只需将结果分配给对象即可。如果没有将对象分配给变量,那么˙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