Katherine, One easy way to do this for small data is by using the append() function (see code below). But, if you have a lot of data, it may be too slow for you. In that case, you can gain some efficiency if you determine in advance how long the vectors will be, then use indexing to fill in the vectors without using the append() function. Or, rewrite the code to be vectorized instead of using a while() loop.
cashflows <- 0 last.coupon <- maturity # create "empty" vectors cashflow.tenure <- character(0) cashflow.nos <- numeric(0) while (last.coupon > settlement) { print(last.coupon) # store the dates cashflow.tenure <- append(cashflow.tenure, last.coupon) last.coupon <- add.months(last.coupon, -12/frequency) cashflows <- cashflows + 1 print(cashflows) # store the cashflow numbers cashflow.nos <- append(cashflow.nos, cashflows) } output.dat <- data.frame(cashflow.tenure, cashflow.nos) output.dat Jean On Thu, Apr 3, 2014 at 5:22 AM, Katherine Gobin <katherine_go...@yahoo.com>wrote: > Dear R forum, > > Following is an customized extract of a code I am working on. > > settlement = as.Date("2013-11-25") > maturity = as.Date("2015-10-01") > coupon = 0.066 > yield = 0.1040 > basis = 1 > frequency = 2 > redemption = 100 > > # __________________________________________________________________ > > add.months = function(date, n) > { > nC <- seq(date, by=paste (n, "months"), length = 2)[2] > fD <- as.Date(strftime(as.Date(date), format='%Y-%m-01')) > C <- (seq(fD, by=paste (n+1, "months"), length = 2)[2])-1 > if(nC>C) return(C) > return(nC) > } > > date.diff = function(end, start, basis=1) { > if (basis != 0 && basis != 4) > return(as.numeric(end - start)) > e <- as.POSIXlt(end) > s <- as.POSIXlt(start) > d <- (360 * (e$year - s$year)) + (30 * (e$mon - s$mon )) + (min(30, > e$mday) - min(30, s$mday)) > > return (d) > } > > cashflows <- 0 > last.coupon <- maturity > while (last.coupon > settlement) { > print(last.coupon) # I need to store these dates > last.coupon <- add.months(last.coupon, -12/frequency) > cashflows <- cashflows + 1 > print(cashflows) # I need to store these cashflow numbers > > } > > The print command causes the following output > > [1] "2015-10-01" > [1] 1 > [1] "2015-04-01" > [1] 2 > [1] "2014-10-01" > [1] 3 > [1] "2014-04-01" > [1] 4 > > My problem is how do I store these print outputs or while the loop is > getting executed, how do I save these to some data.frame say > > output_dat > > cashflow_tenure cashflow_nos > > 1 2015-10-01 1 > 2 2015-04-01 2 > 3 2014-10-01 3 > 4 2014-04-01 4 > > Kindly advise > > With regards > > Katherine > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.