[R] Sequence generation in a table

Petr PIKAL petr.pikal at precheza.cz
Thu Dec 9 14:07:44 CET 2010


Hi

r-help-bounces at r-project.org napsal dne 09.12.2010 13:24:17:

> Dear Sirs,
> 
> I understand these already are numeric values. Sir, Basically I am 
working on 
> Value at Risk for the Bond portfolio using the historical simulation and 
for 
> this I need to find out Marked to Market (MTM) value suing the Present 
Value 
> of the coupon payments for each Bonds (here as an example I have taken 
only 3). 
> 
> What I have done so far is for a given bond I have found no of days left 
for 
> maturity. E.g. in 1st case there are 257 days left for maturity. The 
bond pays
> coupon twice a year and thus on 257th day the bond will mature and I 
will be 
> getting the Principal and final coupon payment. Since teh bond is paying 
the 
> coupons every 6 months, going backward from 257 th day, my earlier 
coupon 
> payment falls on (257 - 180) = 77 days. (However, in above example, I 
have 
> just taken 100 just for example purpose)
> 
> Thus, assuming 100 days, my coupons will be paid on 257, 157,  57days. I 
need 
> to convert these days in terms of years and so when I try to divide yy 
defined as 
> 
> yy <- lapply(c(257, 520,
>  110), seq, to=0, by=-100)
> 
> yy/360, I get following error.
> 
> Error in yy/360 : non-numeric argument to binary operator

Did you bother to read ?lapply or some intro documentation?
> 
> On the other hand, 
> 
> yy[[1]]/365          fetches me 
> 
> [1] 0.7138889 0.4361111 0.1583333

I do not believe it. I got

> yy[[1]]/365
[1] 0.7041096 0.4301370 0.1561644

and as I already told you you can use lapply for almost any kind of 
computation. Just try

> lapply(yy, "/", 365)
[[1]]
[1] 0.7041096 0.4301370 0.1561644

[[2]]
[1] 1.42465753 1.15068493 0.87671233 0.60273973 0.32876712 0.05479452

[[3]]
[1] 0.30136986 0.02739726

Regards
Petr



> 
> 
> Thus, I am trying to obtain the result yy <- lapply(c(257, 520,
>  110), seq, to=0, by=-100) in such a form, so taht I should be able to 
further
> analysis. What I was trying to say is since here I am taking only three 
bonds,
> so I can do it individually, however if there are number of bonds (say 
1000) 
> in the portfolio, my method of converting the days individually is not 
practical.
> 
> I am extremely sorry for the inconvenience caused. I tried to keep my 
problem 
> short in oder not to consume your valuable time.
> 
> Regards
> 
> Vince Pyne
> 
> 
> 
> --- On Thu, 12/9/10, Petr PIKAL <petr.pikal at precheza.cz> wrote:
> 
> From: Petr PIKAL <petr.pikal at precheza.cz>
> Subject: Re: [R] Sequence generation in a table
> To: "Vincy Pyne" <vincy_pyne at yahoo.ca>
> Cc: r-help at r-project.org
> Received: Thursday, December 9, 2010, 12:03 PM
> 
> Hi
> 
> r-help-bounces at r-project.org napsal dne 09.12.2010 12:41:47:
> 
> > Dear Sir,
> > 
> > Sorry to bother you again. Sir, the R code provided by you gives me 
> following output.
> > 
> > > yy <- lapply(c(257, 520,
>  110), seq, to=0, by=-100)
> > > yy
> > [[1]]
> > [1] 257 157  57
> > 
> > [[2]]
> > [1] 520 420 320 220 120  20
> > 
> > [[3]]
> > [1] 110  10
> > 
> > The biggest constraint for me is here as an example I have taken only 
> three 
> > cases i.e. c(257, 520, 110), however in reality I will be dealing with 

> no of 
> > cases and that number is unknown. But your code will certainly 
generate 
> me the
> > required numbers. In above case for doing further calculations, I can 
> define say 
> > 
> > yy1 = as.numeric(yy[[1]])
> > yy2 = as.numeric(yy[2]])
> > yy3 = as.numeric(yy[[3]])
> 
> Why? Those values are already numeric.
> 
> lapply(yy, is.numeric)
> 
> [[1]]
> [1] TRUE
> 
> [[2]]
> [1] TRUE
> 
> [[3]]
> [1] TRUE
> 
> and you can use the same construction to perform almost any operation on 

> list.
> 
> lapply(yy, max)
> lapply(yy,
>  mean)
> lapply(yy, sd)
> lapply(yy, t.test)
> 
> Regards
> Petr
> 
> 
> > 
> > But when the number of cases are unknown, perhaps this is not the 
> practical 
> > way of me defining individually. So is there any way that I can have 
all 
> the 
> > sequence numbers generated can be accommodated in a single dataframe. 
I 
> > sincerely apologize for disturbing you Sir and hope I am able to put 
up 
> my 
> > problem in a proper manner.
> > 
> > Regards
> > 
> > Vincy Pyne
> > 
> > 
> > --- On Thu, 12/9/10, Jan van der Laan <rhelp at eoos.dds.nl> wrote:
> > 
> > From: Jan van der Laan <rhelp at eoos.dds.nl>
> > Subject: Re: [R] Sequence generation in a table
> > To: r-help at r-project.org, vincy_pyne at yahoo.ca
> > Received: Thursday, December 9, 2010, 10:57 AM
> > 
> > Vincy,
> > 
> > I suppose the following does what you want. yy is now a list which 
> allows for 
> > differing lengths of the vectors.
> > 
> > > yy <- lapply(c(257, 520, 110), seq, to=0, by=-100)
> > > yy[[1]]
> > [1] 257 157  57
> > > yy[[2]]
> > [1] 520 420 320 220 120  20
> > 
> > 
> > Regards,
> > Jan
> > 
> > 
> > On 9-12-2010 11:40, Vincy Pyne wrote:
> > > c(257, 520, 110)
> > 
> > 
> > 
> >    [[alternative HTML version deleted]]
> > 
> > ______________________________________________
> > R-help at 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 at 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.



More information about the R-help mailing list