[R] How to store interim print results
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(nCC) 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.
Re: [R] How to store interim print results
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(nCC) 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.comwrote: 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(nCC) 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_tenurecashflow_nos 1 2015-10-011 2 2015-04-012 3 2014-10-013 4 2014-04-014 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.
Re: [R] How to store interim print results
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.comwrote: 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(nCC) 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_tenurecashflow_nos 1 2015-10-011 2 2015-04-012 3 2014-10-013 4 2014-04-014 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.
Re: [R] How to store interim print results
Dear Jean Thanks a lot for this solution. Its very useful. I did only one small change and defined cashflow.tenure - numeric(0) instead of character(0). This helps me in further numerical calculations using these dates like finding the difference between two dates etc. Thanks again, Regards Katherine On Thursday, 3 April 2014 6:58 PM, Adams, Jean jvad...@usgs.gov wrote: 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(nCC) 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.
Re: [R] How to store interim print results
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(nCC) 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(nCC) 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.