【发布时间】:2021-05-17 10:28:56
【问题描述】:
这是我制作的一个函数。它并不复杂。基本上使用您输入的数据在数据表中制作向量并加起来一行。它会吐出一个列表,该列表是行和数据表的总和!
q4PV<- function(cfs,ttms,i) {
cash_flows<-cfs
cash_flows<-data.frame(cash_flows)
cash_flows$ttm<-ttms
cash_flows$rates<-i/100
cash_flows$present_values<-cash_flows$cash_flows*(1/(1+cash_flows$rates)^cash_flows$ttm)
results<-list(Data=cash_flows,Total_Present_Value=sum(cash_flows$present_values))
return(results)
}
# Here I gave it some info and it worked great!
q4PV(cfs=c(1100, -500, 1900, 1800, 2000, 100, 1400, 1900, 400, 800),
ttms=c(3,4,5,6,7,10,11,14,16,18),
i=c(15,15,15,15,15,15,15,15,15,15))
# To my surprise, it spit this out, as I wanted:
$Data
cash_flows ttm rates present_values
1 1100 3 0.15 723.26786
2 -500 4 0.15 -285.87662
3 1900 5 0.15 944.63580
4 1800 6 0.15 778.18967
5 2000 7 0.15 751.87408
6 100 10 0.15 24.71847
7 1400 11 0.15 300.92051
8 1900 14 0.15 268.52445
9 400 16 0.15 42.74591
10 800 18 0.15 64.64410
$Total_Present_Value
[1] 3613.644
这是我的问题,我是 R 新手,能够使用这张表会很棒。但它是函数的局部,有没有办法解决这个问题?!因此,当它已经为我完成时,我不必创建另一个 data.table!
谢谢 亚伦S
【问题讨论】:
-
my_table <- q4PV(YOUR_PARAMETERS)
标签: r function data.table local