Wie lese ich den Amortisationsplan von tvm()
im Paket financial
in einen Datenrahmen?
Beispielcode
y=tvm(pv=10000,i=10,n=10,pmt=NA)
summary.tvm(y)
Ergebnisse
> 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
Es ist eindeutig diese Zusammenfassung, die ich einem Datenrahmen zuordnen muss, wenn ich den Quellcode der Funktion überprüfe summary.tvm()
zeigt es das.
> summary.tvm
function (object, row = 1, ...)
{
cat("nAmortization Tablenn")
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>
Ich denke ich muss irgendwie extrahieren res
und ordnen Sie es einem Datenrahmen zu. Wie macht man das? Ich habe die Klasse des geprüft summary.tvm
Ausgabe zeigt es ein matrix
.
Antworten:
1 für die Antwort № 1Ordnen Sie das Ergebnis einfach einem Objekt zu. Der "unsichtbare" Teil unterdrückt lediglich das Drucken des Objekts, wenn es keiner Variablen zugewiesen ist.
out <- summary.tvm(y)
Das Ergebnis sollte eine Matrix sein. Warum glauben Sie, es sollte etwas anderes sein? Wenn Sie einen Datenrahmen haben möchten, versuchen Sie es as.data.frame(out)
.
In einem kurzen Beispiel:
> 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