我在R。
中有下面提到的数据框ACCOUNT_ID LOAN_AMOUNT DISBURSEMENT_DATE LOAN_DUE_DATE LOAN_INSTALLMENT
ID123 100000 08/01/2018 00:00 08/02/2018 00:00 9433
ID123 100000 08/01/2018 00:00 08/03/2018 00:00 9433
ID123 100000 08/01/2018 00:00 08/04/2018 00:00 9433
ID123 100000 08/01/2018 00:00 08/05/2018 00:00 9433
ID123 100000 08/01/2018 00:00 08/06/2018 00:00 9433
ID123 100000 08/01/2018 00:00 08/07/2018 00:00 9433
ID123 100000 08/01/2018 00:00 08/08/2018 00:00 9433
ID123 100000 08/01/2018 00:00 08/09/2018 00:00 9433
ID123 100000 08/01/2018 00:00 08/10/2018 00:00 9433
ID123 100000 08/01/2018 00:00 08/11/2018 00:00 9433
ID123 100000 08/01/2018 00:00 08/12/2018 00:00 9433
ID123 100000 08/01/2018 00:00 08/01/2019 00:00 9433
ID456 200000 10/01/2018 00:00 10/02/2018 00:00 11533
ID456 200000 10/01/2018 00:00 10/03/2018 00:00 11533
ID456 200000 10/01/2018 00:00 10/04/2018 00:00 11533
ID456 200000 10/01/2018 00:00 10/05/2018 00:00 11533
ID456 200000 10/01/2018 00:00 10/06/2018 00:00 11533
ID456 200000 10/01/2018 00:00 10/07/2018 00:00 11533
ID456 200000 10/01/2018 00:00 10/08/2018 00:00 11533
ID456 200000 10/01/2018 00:00 10/09/2018 00:00 11533
ID456 200000 10/01/2018 00:00 10/10/2018 00:00 11533
ID456 200000 10/01/2018 00:00 10/11/2018 00:00 11533
ID456 200000 10/01/2018 00:00 10/12/2018 00:00 11533
ID456 200000 10/01/2018 00:00 10/01/2019 00:00 11533
ID456 200000 10/01/2018 00:00 10/02/2019 00:00 11533
ID456 200000 10/01/2018 00:00 10/03/2019 00:00 11533
ID456 200000 10/01/2018 00:00 10/04/2019 00:00 11533
ID456 200000 10/01/2018 00:00 10/05/2019 00:00 11533
ID456 200000 10/01/2018 00:00 10/06/2019 00:00 11533
ID456 200000 10/01/2018 00:00 10/07/2019 00:00 11533
ID456 200000 10/01/2018 00:00 10/08/2019 00:00 11533
ID456 200000 10/01/2018 00:00 10/09/2019 00:00 11533
ID456 200000 10/01/2018 00:00 10/10/2019 00:00 11533
ID456 200000 10/01/2018 00:00 10/11/2019 00:00 11533
ID456 200000 10/01/2018 00:00 10/12/2019 00:00 11533
ID456 200000 10/01/2018 00:00 10/01/2020 00:00 11533
我有哪些功能可以计算IRR:
irrcalc <- function(cashflow, dates){
irr <- xirr(cashflow, dates)
return(irr)
}
但是为了使用这个功能,我希望我的日期成名在下面提到的结构中:
cashflow <-c(-100000,9433,9433,9433,9433,9433,9433,9433,9433,9433,9433,9433,9433)
dates <- as.Date(c("08/01/2018","08/02/2018","08/03/2018","08/04/2018","08/05/2018","08/06/2018","08/07/2018","08/08/2018","08/09/2018","08/10/2018","08/11/2018","08/12/2018","08/01/2019"))
irr <- xirr(cashflow, dates)
为此,我有一个代码,但它在“现金流”中的第一个值是正确的,应该是-100000但其余24个vales从其他Account_ID(ID456)捕获并返回现金流&lt; - c(-100000,11533,11533 ,..)而不是现金流&lt; -c(-100000,9433,9433,...)
请检查以下代码:
NEWDATAFRAME <- data.frame(row.names = NULL)
nrow <- 1
for (acct in unique(DFNAME[,"ACCOUNT_ID"])){
cashflow <- DFNAME[DFNAME[,"ACCOUNT_ID"] == acct,"LOAN_INSTALLMENT"]
cashflow[1] <- -DFNAME[1,"LOAN_AMOUNT"]
dates <- DFNAME[DFNAME[,"ACCOUNT_ID"] == acct,"LOAN_DUE_DATE"]
dates[1] <- DFNAME[1,"DISBURSEMENT_DATE"]
NEWDATAFRAME[nrow,"ACCOUNT_ID"] <- acct
NEWDATAFRAME[nrow,"IRR"] <- irrcalc(cashflow,dates)
nrow <- nrow + 1
}
答案 0 :(得分:0)
我不知道函数xirr()
,但是如果我的理解是正确的,下面的代码应该以您需要的格式存储数据。
我已将您的数据导入data.frame d
,并将日期转换为日期格式(使用as.Date(d$DISBURSEMENT_DATE, "%d/%m/%Y")
)。这就是我所拥有的:
'data.frame': 36 obs. of 5 variables:
$ ACCOUNT_ID : Factor w/ 2 levels "ID123","ID456": 1 1 1 1 1 1 1 1 1 1 ...
$ LOAN_AMOUNT : int 100000 100000 100000 100000 100000 100000 100000 100000 100000 100000 ...
$ DISBURSEMENT_DATE: Date, format: "2018-01-08" "2018-01-08" "2018-01-08" ...
$ LOAN_DUE_DATE : Date, format: "2018-02-08" "2018-03-08" "2018-04-08" ...
$ LOAN_INSTALLMENT : int 9433 9433 9433 9433 9433 9433 9433 9433 9433 9433 ...
这是代码
NEWDATAFRAME <- {}
for(i in unique(d$ACCOUNT_ID)){
sub_d <- d[d$ACCOUNT_ID == i,]
cashflow <- c(-sub_d$LOAN_AMOUNT[1], sub_d$LOAN_INSTALLMENT)
dates <- c(sub_d$DISBURSEMENT_DATE[1], sub_d$LOAN_DUE_DATE)
# xirr(cashflow, dates) can be added within the data.frame() call below
NEWDATAFRAME <- rbind(NEWDATAFRAME, data.frame(cashflow, dates, id = i))
}
这是输出
> NEWDATAFRAME
cashflow dates id
1 -100000 2018-01-08 ID123
2 9433 2018-02-08 ID123
3 9433 2018-03-08 ID123
4 9433 2018-04-08 ID123
5 9433 2018-05-08 ID123
6 9433 2018-06-08 ID123
7 9433 2018-07-08 ID123
8 9433 2018-08-08 ID123
9 9433 2018-09-08 ID123
10 9433 2018-10-08 ID123
11 9433 2018-11-08 ID123
12 9433 2018-12-08 ID123
13 9433 2019-01-08 ID123
14 -200000 2018-01-10 ID456
15 11533 2018-02-10 ID456
16 11533 2018-03-10 ID456
17 11533 2018-04-10 ID456
18 11533 2018-05-10 ID456
19 11533 2018-06-10 ID456
20 11533 2018-07-10 ID456
21 11533 2018-08-10 ID456
22 11533 2018-09-10 ID456
23 11533 2018-10-10 ID456
24 11533 2018-11-10 ID456
25 11533 2018-12-10 ID456
26 11533 2019-01-10 ID456
27 11533 2019-02-10 ID456
28 11533 2019-03-10 ID456
29 11533 2019-04-10 ID456
30 11533 2019-05-10 ID456
31 11533 2019-06-10 ID456
32 11533 2019-07-10 ID456
33 11533 2019-08-10 ID456
34 11533 2019-09-10 ID456
35 11533 2019-10-10 ID456
36 11533 2019-11-10 ID456
37 11533 2019-12-10 ID456
38 11533 2020-01-10 ID456