Dear R helpers (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.) 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. My Code - ## ONS - PPA duration = function(par_value, coupon_rate, frequency_copoun, tenure, ytm) { macaulay_duration = NULL modified_duration = NULL freq_coupon_new = NULL if(frequency_copoun <= 0) { freq_coupon_new = 365 } if(frequency_copoun > 0 & frequency_copoun <= 1) { freq_coupon_new = 12 } if(frequency_copoun > 1 & frequency_copoun <= 2) { freq_coupon_new = 4 } if(frequency_copoun > 2 & frequency_copoun <= 3) { freq_coupon_new = 2 } if(frequency_copoun > 3 & frequency_copoun <= 4) { freq_coupon_new = 1 } ## COMPUTATIONS terms_coupon_payment = (seq(1/freq_coupon_new, tenure, by = 1/freq_coupon_new))*freq_coupon_new coupon = coupon_rate*par_value/100 coupon_amount = coupon/(freq_coupon_new) cash_flow1 = rep(c(coupon_amount), (tenure*freq_coupon_new - 1)) cash_flow2 = par_value + coupon_amount cash_flow = c(cash_flow1, cash_flow2) ytm_effective = ((1+ytm/100)^(1/freq_coupon_new))-1 pv = NULL for (i in 1:(tenure*freq_coupon_new)) { pv[i] = cash_flow[i] / ((1+ytm_effective)^terms_coupon_payment[i]) } macaulay_duration = sum(pv*terms_coupon_payment)/sum(pv) modified_duration = macaulay_duration / (1+(ytm_effective)/freq_coupon_new) return(data.frame(macaulay_duration, modified_duration)) }
### For a given data say result = duration(par_value = 1000, coupon_rate = 10, frequency_copoun = 0, tenure = 5, ytm = 12) I get the output as macaulay_duration modified_duration 1 1423.797 1423.795 ## __________________________________________________________________ ## MY PROBLEM If instead of having only one set of data, suppose I have multiple data (say as given below in a csv file), I am not able to get these results. Suppose my 'input.csv' file is as given below. par_value coupon_rate frequency_copoun tenure ytm 1000 10 0 5 12 100 7 1 8 11 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 request you to please guide me. I undesratnd its not my right to seek help, but this is for the third time I am requesting to guide me. Thanking you all Regards Madhavi The INTERNET now has a personality. YOURS! See your Yahoo! Homepage. [[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.