Dear Sir,

Thanks a lot for your guidance and efforts. Appreciate it.

Thanks again.

Katherine
On Thursday, 3 April 2014 6:55 PM, jim holtman <jholt...@gmail.com> wrote:
 
This will get you close:

> 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)
+ }
> 
> output <- capture.output({  # collect the print output
+  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   
+   }
+   
+ })  
> 
> # remove line numbers
> output <- sub("^....", "", output)
> 
> # remove extra quotes
> output <- gsub('"', '', output)
> 
> 
> # now read in the data
> report <- matrix(output, ncol = 2, byrow = TRUE)
> 
> report
     [,1]         [,2]
[1,] "2015-10-01" "1" 
[2,] "2015-04-01" "2" 
[3,] "2014-10-01" "3" 
[4,] "2014-04-01" "4" 
> 
> 




Jim Holtman
Data Munger Guru
 
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.


On Thu, Apr 3, 2014 at 6: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.

Reply via email to