[R] Please Please Please Help me (Changed to: calling a function with a list of parameters)
Dieter Menne
dieter.menne at menne-biomed.de
Wed Jan 20 09:41:00 CET 2010
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.
More information about the R-help
mailing list