Dear R helpers I have following input f = c(257, 520, 110). I need to generate a decreasing sequence (decreasing by 100) which will give me an input (in a tabular form) like 257, 157, 57 520, 420, 320, 220, 120, 20 110, 10 I tried the following R code f = c(257, 520, 110) yy = matrix(data = NA, nrow = 3, ncol = 6) for (i in 1:3) { value = NULL for (j in 1 : 6) { value = c(ans, seq(f[i], 1, by = -100)) } yy[i,] = ans[i,j] } I get following message Error in ans[i, j] : incorrect number of dimensions. Also, I understand above logic will generate a result in (3 by 6) matrix format, while I need to generate only 3 numbers pertaining to first no. i.e. 257, 6 nos. beginning from 520, and only 2 numbers beginning from 110. I also tried tapply etc. Please guide Vincy [[alternative HTML version deleted]]
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)
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 On the other hand, yy[[1]]/365 fetches me [1] 0.7138889 0.4361111 0.1583333 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@precheza.cz> wrote: From: Petr PIKAL <petr.pikal@precheza.cz> Subject: Re: [R] Sequence generation in a table To: "Vincy Pyne" <vincy_pyne@yahoo.ca> Cc: r-help@r-project.org Received: Thursday, December 9, 2010, 12:03 PM Hi r-help-bounces@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 mefollowing 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 onlythree> cases i.e. c(257, 520, 110), however in reality I will be dealing withno of> cases and that number is unknown. But your code will certainly generateme the> required numbers. In above case for doing further calculations, I candefine 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 thepractical> way of me defining individually. So is there any way that I can have allthe> sequence numbers generated can be accommodated in a single dataframe. I > sincerely apologize for disturbing you Sir and hope I am able to put upmy> problem in a proper manner. > > Regards > > Vincy Pyne > > > --- On Thu, 12/9/10, Jan van der Laan <rhelp@eoos.dds.nl> wrote: > > From: Jan van der Laan <rhelp@eoos.dds.nl> > Subject: Re: [R] Sequence generation in a table > To: r-help@r-project.org, vincy_pyne@yahoo.ca > Received: Thursday, December 9, 2010, 10:57 AM > > Vincy, > > I suppose the following does what you want. yy is now a list whichallows 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@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.[[alternative HTML version deleted]]
On Thu, Dec 9, 2010 at 1:24 PM, Vincy Pyne <vincy_pyne at yahoo.ca> wrote:> 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 > > On the other hand, > > yy[[1]]/365????????? fetches me > > [1] 0.7138889 0.4361111 0.1583333 >What about this?> bond.fun <- function(x, days=360) {x/days} > lapply(yy, bond.fun)[[1]] [1] 0.7138889 0.4361111 0.1583333 [[2]] [1] 1.44444444 1.16666667 0.88888889 0.61111111 0.33333333 0.05555556 [[3]] [1] 0.30555556 0.02777778> lapply(yy, bond.fun, days=366)[[1]] [1] 0.7021858 0.4289617 0.1557377 [[2]] [1] 1.42076503 1.14754098 0.87431694 0.60109290 0.32786885 0.05464481 [[3]] [1] 0.30054645 0.02732240
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 amworking on> Value at Risk for the Bond portfolio using the historical simulation andfor> this I need to find out Marked to Market (MTM) value suing the PresentValue> of the coupon payments for each Bonds (here as an example I have takenonly 3).> > What I have done so far is for a given bond I have found no of days leftfor> maturity. E.g. in 1st case there are 257 days left for maturity. Thebond pays> coupon twice a year and thus on 257th day the bond will mature and Iwill be> getting the Principal and final coupon payment. Since teh bond is payingthe> coupons every 6 months, going backward from 257 th day, my earliercoupon> payment falls on (257 - 180) = 77 days. (However, in above example, Ihave> just taken 100 just for example purpose) > > Thus, assuming 100 days, my coupons will be paid on 257, 157, 57days. Ineed> to convert these days in terms of years and so when I try to divide yydefined 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 operatorDid you bother to read ?lapply or some intro documentation?> > On the other hand, > > yy[[1]]/365 fetches me > > [1] 0.7138889 0.4361111 0.1583333I 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 tofurther> analysis. What I was trying to say is since here I am taking only threebonds,> so I can do it individually, however if there are number of bonds (say1000)> in the portfolio, my method of converting the days individually is notpractical.> > I am extremely sorry for the inconvenience caused. I tried to keep myproblem> 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 certainlygenerate> 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 haveall> the > > sequence numbers generated can be accommodated in a single dataframe.I> > sincerely apologize for disturbing you Sir and hope I am able to putup> 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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.