# [R] modifying list in loop

Sat Apr 1 23:07:23 CEST 2017

```Hello,

I don't understand your functions but neither of them returns a value.
And in the loop that calls them

for(period in 1:1){
BeginBal(Deal, period)
}

you don't assign the value of BeginBal to anything, so Deal is never
updated.

The corrected functions and a way of calling them would be

BeginBal <- function(deal,period){
numtranches = length(deal)
tranches = seq(numtranches,1,-1)
for(tranche in seq_along(tranches)){
cashflow <- NULL
if(period == 1) {
cashflow <- c(deal[[tranche]]\$CashFlow\$BeginBal, deal[[tranche]]\$CurrBal)
} else {
cashflow <- c(deal[[tranche]]\$CashFlow\$BeginBal,
deal[[tranche]]\$CashFlow\$EndingBal[[period -1]])
}
deal[[tranche]]\$CashFlow\$BeginBal <- cashflow
print(deal[[tranche]]\$CashFlow\$BeginBal)
}
deal
}

BeginBal2 <- function(deal,period){
numtranches = length(deal)
tranches = seq(numtranches,1,-1)
for(tranche in seq_along(tranches)){
cashflow <- NULL
if(period == 1) {
cashflow <- c(deal[[tranche]]\$CashFlow\$BeginBal, deal[[tranche]]\$CurrBal)
} else {
cashflow <- c(deal[[tranche]]\$CashFlow\$BeginBal,
deal[[tranche]]\$CashFlow\$EndingBal[[period -1]])
}
deal[[tranche]]\$CashFlow\$BeginBal <- cashflow
print(deal[[tranche]]\$CashFlow\$BeginBal)
}
deal
}

result <- lapply(1:1, function(i) BeginBal(Deal, i))
result

Hope this helps,

Em 01-04-2017 18:05, Glenn Schultz escreveu:
> All,
>
> I am working on structuring a FHLMC credit risk transfer deal.  I have
> the deal modeled as a list of lists as shown below.  I would like to
> fill in the CashFlows.  The first step is simply to assign the current
> balance to the first position in CashFlow\$BeginBal.  I am using the
> below function to update the values in the list.  The function works
> properly but the list values are not updated.  Any help is appreciated.
>
> Best,
> Glenn
>
> BeginBal <- function(deal,period){
> numtranches = length(deal)
> tranches = seq(numtranches,1,-1)
> for(tranche in seq_along(tranches)){
> cashflow <- NULL
> if(period == 1) {cashflow <- c(deal[[tranche]]\$CashFlow\$BeginBal,
> deal[[tranche]]\$CurrBal)
> } else {
> cashflow <- c(deal[[tranche]]\$CashFlow\$BeginBal,
> deal[[tranche]]\$CashFlow\$EndingBal[[period -1]])}
> deal[[tranche]]\$CashFlow\$BeginBal <- cashflow
> print(deal[[tranche]]\$CashFlow\$BeginBal)
> }
> }
>
> for(period in 1:1){
> BeginBal(Deal, period)
> }
>
> structure(list(OC = structure(list(Tranche = "OC", Cusip = NULL, Coupon
> = 0L, Index = NULL, Margin = NULL, OrigBal = 25905603, CurrBal =
> 25905603, CashFlow = structure(list(BeginBal = list(), WriteDown =
> list(), WriteUp = list(), EndBal = list()), .Names = c("BeginBal",
> "WriteDown", "WriteUp", "EndBal"))), .Names = c("Tranche", "Cusip",
> "Coupon", "Index", "Margin", "OrigBal", "CurrBal", "CashFlow"
> )), B2H = structure(list(Tranche = "B2H", Cusip = NULL, Coupon =
> 10.7794, Index = "LIBOR1M", Margin = 10, OrigBal = 152827059, CurrBal =
> 152827059, IssueDate = "02-01-2017", DatedDate = "02-01-2017",
> LastPmtDate = "02-01-2017", NextPmtDate = "02-25-2017", MaturityDate =
> "07-25-2029", CashFlow = structure(list(BeginBal = list(), Interest =
> list(), WriteDown = list(), WriteUp = list(), SeniorReduction = list(),
> SubReduction = list(), EndBal = list()), .Names = c("BeginBal",
> "Interest", "WriteDown", "WriteUp", "SeniorReduction", "SubReduction",
> "EndBal"))), .Names = c("Tranche", "Cusip", "Coupon", "Index", "Margin",
> "OrigBal", "CurrBal", "IssueDate", "DatedDate", "LastPmtDate",
> "NextPmtDate", "MaturityDate", "CashFlow")), B2 = structure(list(
> Tranche = "B2", Cusip = NULL, Coupon = 10.77944, Index = "LIBOR1M",
> Margin = 10, OrigBal = 1.7e+07, CurrBal = 1.7e+07, IssueDate =
> "02-01-2017", DatedDate = "02-01-2017", LastPmtDate = "02-01-2017",
> NextPmtDate = "02-25-2017", MaturityDate = "07-25-2029", CashFlow =
> structure(list(BeginBal = list(), Interest = list(), WriteDown = list(),
> WriteUp = list(), SeniorReduction = list(), SubReduction = list(),
> EndBal = list()), .Names = c("BeginBal", "Interest", "WriteDown",
> "WriteUp", "SeniorReduction", "SubReduction", "EndBal"))), .Names =
> c("Tranche", "Cusip", "Coupon", "Index", "Margin", "OrigBal", "CurrBal",
> "IssueDate", "DatedDate", "LastPmtDate", "NextPmtDate", "MaturityDate",
> "CashFlow")), B1H = structure(list(
> Tranche = "B1H", Cusip = NULL, Coupon = 5.72944, Index = "LIBOR1M",
> Margin = 4.95, OrigBal = 49827059, CurrBal = 49827059, IssueDate =
> "02-01-2017", DatedDate = "02-01-2017", LastPmtDate = "02-01-2017",
> NextPmtDate = "02-25-2017", MaturityDate = "07-25-2029", CashFlow =
> structure(list(BeginBal = list(), Interest = list(), WriteDown = list(),
> WriteUp = list(), SeniorReduction = list(), SubReduction = list(),
> EndBal = list()), .Names = c("BeginBal", "Interest", "WriteDown",
> "WriteUp", "SeniorReduction", "SubReduction", "EndBal"))), .Names =
> c("Tranche", "Cusip", "Coupon", "Index", "Margin", "OrigBal", "CurrBal",
> "IssueDate", "DatedDate", "LastPmtDate", "NextPmtDate", "MaturityDate",
> "CashFlow")), B1 = structure(list(
> Tranche = "B1", Cusip = NULL, Coupon = 5.72944, Index = "LIBOR1M",
> Margin = 4.95, OrigBal = 1.2e+08, CurrBal = 1.2e+08, IssueDate =
> "02-01-2017", DatedDate = "02-01-2017", LastPmtDate = "02-01-2017",
> NextPmtDate = "02-25-2017", MaturityDate = "07-25-2029", CashFlow =
> structure(list(BeginBal = list(), Interest = list(), WriteDown = list(),
> WriteUp = list(), SeniorReduction = list(), SubReduction = list(),
> EndBal = list()), .Names = c("BeginBal", "Interest", "WriteDown",
> "WriteUp", "SeniorReduction", "SubReduction", "EndBal"))), .Names =
> c("Tranche", "Cusip", "Coupon", "Index", "Margin", "OrigBal", "CurrBal",
> "IssueDate", "DatedDate", "LastPmtDate", "NextPmtDate", "MaturityDate",
> "CashFlow")), M2H = structure(list(
> Tranche = "M2H", Cusip = NULL, Coupon = 4.02944, Index = "LIBOR1M",
> Margin = 3.25, OrigBal = 151463884, CurrBal = 151463884, IssueDate =
> "02-01-2017", DatedDate = "02-01-2017", LastPmtDate = "02-01-2017",
> NextPmtDate = "02-25-2017", MaturityDate = "07-25-2029", CashFlow =
> structure(list(BeginBal = list(), Interest = list(), WriteDown = list(),
> WriteUp = list(), SeniorReduction = list(), SubReduction = list(),
> EndBal = list()), .Names = c("BeginBal", "Interest", "WriteDown",
> "WriteUp", "SeniorReduction", "SubReduction", "EndBal"))), .Names =
> c("Tranche", "Cusip", "Coupon", "Index", "Margin", "OrigBal", "CurrBal",
> "IssueDate", "DatedDate", "LastPmtDate", "NextPmtDate", "MaturityDate",
> "CashFlow")), M2 = structure(list(
> Tranche = "M2", Cusip = NULL, Coupon = 4.02944, Index = "LIBOR1M",
> Margin = 3.25, OrigBal = 3.75e+08, CurrBal = 3.75e+08, IssueDate =
> "02-01-2017", DatedDate = "02-01-2017", LastPmtDate = "02-01-2017",
> NextPmtDate = "02-25-2017", MaturityDate = "07-25-2029", CashFlow =
> structure(list(BeginBal = list(), Interest = list(), WriteDown = list(),
> WriteUp = list(), SeniorReduction = list(), SubReduction = list(),
> EndBal = list()), .Names = c("BeginBal", "Interest", "WriteDown",
> "WriteUp", "SeniorReduction", "SubReduction", "EndBal"))), .Names =
> c("Tranche", "Cusip", "Coupon", "Index", "Margin", "OrigBal", "CurrBal",
> "IssueDate", "DatedDate", "LastPmtDate", "NextPmtDate", "MaturityDate",
> "CashFlow")), M1H = structure(list(
> Tranche = "M1H", Cusip = NULL, Coupon = 1.97944, Index = "LIBOR1M",
> Margin = 1.2, OrigBal = 117584943, CurrBal = 117584943, IssueDate =
> "02-01-2017", DatedDate = "02-01-2017", LastPmtDate = "02-01-2017",
> NextPmtDate = "02-25-2017", MaturityDate = "07-25-2029", CashFlow =
> structure(list(BeginBal = list(), Interest = list(), WriteDown = list(),
> WriteUp = list(), SeniorReduction = list(), SubReduction = list(),
> EndBal = list()), .Names = c("BeginBal", "Interest", "WriteDown",
> "WriteUp", "SeniorReduction", "SubReduction", "EndBal"))), .Names =
> c("Tranche", "Cusip", "Coupon", "Index", "Margin", "OrigBal", "CurrBal",
> "IssueDate", "DatedDate", "LastPmtDate", "NextPmtDate", "MaturityDate",
> "CashFlow")), M1 = structure(list(
> Tranche = "M1", Cusip = NULL, Coupon = 1.97944, Index = "LIBOR1M",
> Margin = 1.2, OrigBal = 2.9e+08, CurrBal = 2.9e+08, IssueDate =
> "02-01-2017", DatedDate = "02-01-2017", LastPmtDate = "02-01-2017",
> NextPmtDate = "02-25-2017", MaturityDate = "07-25-2029", CashFlow =
> structure(list(BeginBal = list(), Interest = list(), WriteDown = list(),
> WriteUp = list(), SeniorReduction = list(), SubReduction = list(),
> EndBal = list()), .Names = c("BeginBal", "Interest", "WriteDown",
> "WriteUp", "SeniorReduction", "SubReduction", "EndBal"))), .Names =
> c("Tranche", "Cusip", "Coupon", "Index", "Margin", "OrigBal", "CurrBal",
> "IssueDate", "DatedDate", "LastPmtDate", "NextPmtDate", "MaturityDate",
> "CashFlow")), A1H = structure(list(
> Tranche = "AH", Cusip = NULL, Coupon = 0L, Index = "LIBOR1M", Margin =
> 1.2, OrigBal = 32691709407, CurrBal = 32691709407, IssueDate =
> "02-01-2017", DatedDate = "02-01-2017", LastPmtDate = "02-01-2017",
> NextPmtDate = "02-25-2017", MaturityDate = "07-25-2029", CashFlow =
> structure(list(BeginBal = list(), Interest = list(), WriteDown = list(),
> WriteUp = list(), SeniorReduction = list(), SubReduction = list(),
> EndBal = list()), .Names = c("BeginBal", "Interest", "WriteDown",
> "WriteUp", "SeniorReduction", "SubReduction", "EndBal"))), .Names =
> c("Tranche", "Cusip", "Coupon", "Index", "Margin", "OrigBal", "CurrBal",
> "IssueDate", "DatedDate", "LastPmtDate", "NextPmtDate", "MaturityDate",
> "CashFlow"))), .Names = c("OC", "B2H", "B2", "B1H", "B1", "M2H", "M2",
> "M1H", "M1", "A1H"))
>
>
>
> BeginBal <- function(deal,period){
> numtranches = length(deal)
> tranches = seq(numtranches,1,-1)
> for(tranche in seq_along(tranches)){
> cashflow <- NULL
> if(period == 1) {cashflow <- c(deal[[tranche]]\$CashFlow\$BeginBal,
> deal[[tranche]]\$CurrBal)
> } else {
> cashflow <- c(deal[[tranche]]\$CashFlow\$BeginBal,
> deal[[tranche]]\$CashFlow\$EndingBal[[period -1]])}
> deal[[tranche]]\$CashFlow\$BeginBal <- cashflow
> print(deal[[tranche]]\$CashFlow\$BeginBal)
> }
> }
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help