An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20100119/9218858b/attachment.pl>
Please Please Please Help me!!
3 messages · Madhavi Bhave, Dieter Menne, Liviu Andronic
Please, do not write "Help, Help" but give a meaningful header. Most people here a specialized and check the posts they understand best.
Madhavi Bhave wrote:
? (I have already written the required R code which is giving me correct results for a given single set of data. I just wish to wish to use it for multiple data.)
It's fine that you posted the working part; in general, for posting you should simplify the thing that do work to the bare bones (see example below)
Madhavi Bhave wrote:
I have defined a function (as given below) which helps me calculate Macaulay Duration and Modified Duration and its working fine with given set of data. I just want to use this code for multiple data so that I get an output something like ???????? maculay_duration??????????????????? modified_duration ? ????????? 1423.797?????????????????????????????????? 1423.795 ? ???????????? 44.339?????????????????????????????????????? 44.307 ?
The most elegant approach uses package plyr; you even get the function
parameters in your output.
library(plyr)
# simplified version of your original function
duration = function(par_value, coupon_rate)
{
data.frame(macaulay_duration=2*par_value,modified_duration=3*coupon_rate)
}
# use read.table in your application instead of the line below
pars = data.frame(par_value=c(1000,100), coupon_rate=c(10,7))
mdply(pars,.fun=duration)
# par_value coupon_rate macaulay_duration modified_duration
#1 1000 10 2000 30
#2
View this message in context: http://n4.nabble.com/Please-Please-Please-Help-me-tp1018209p1018251.html Sent from the R help mailing list archive at Nabble.com.
Hello
On Wed, Jan 20, 2010 at 7:00 AM, Madhavi Bhave <madhavi_bhave at yahoo.com> wrote:
Sir, I am not asking for the modification of existing code as it is running fine with a single set of data (and I have checked that the output tallies with other methods). I just want to use this code for multiple data so that I get an output something like ???????? maculay_duration??????????????????? modified_duration ????????? 1423.797?????????????????????????????????? 1423.795 ???????????? 44.339?????????????????????????????????????? 44.307
I am not sure that this would be the most efficient, but see if
something similar does the job for you:
x <- matrix(NA, 5,2); x
for (i in 1:5){
x1 <- cbind(mean(rnorm(100)), mean(runif(100))) ## use duration()
instead of cbind() and r*() funs
x[i, ] <- x1
}
x
Liviu